Board logo

標題: [發問] 如何利用VBA刪除字串前後的數字或英文字串 [打印本頁]

作者: jsleee    時間: 2024-3-30 19:52     標題: 如何利用VBA刪除字串前後的數字或英文字串

各位先進好,
請教一個問題,如果我有字串是 數字+中文字+英文字
如果我想要把中文字的部分從字串中取出來,不知有甚麼方式?
例如:1100中文中文中文...AABBCC,只想要取出 "中文中文中文..." 這段,該如何寫程式碼?
請先進不吝指導,謝謝

JS
作者: quickfixer    時間: 2024-3-30 22:09

回復 1# jsleee


    [attach]37641[/attach]

Function test(s As String) As String
    Dim reg As Object
    Set reg = CreateObject("vbscript.regexp")
    With reg
        .Global = True
        .Pattern = "[A-Za-z0-9]+"
        test = .Replace(s, "")
    End With
End Function
作者: jsleee    時間: 2024-3-30 23:27

回復 2# quickfixer    

quickfixer您好
  不知能否提供一些解說,我不太能理解您的程式碼。
   感激不盡,謝謝
JS
作者: quickfixer    時間: 2024-3-30 23:46

回復 3# jsleee

撰寫函式程式
https://learn.microsoft.com/zh-tw/office/vba/language/concepts/getting-started/writing-a-function-procedure
    Function test(s As String) As String
    定義
    Dim reg As Object
正規表示式物件
    Set reg = CreateObject("vbscript.regexp")

    With reg
全局匹配模式
        .Global = True
匹配模式 大小寫a~z,數字 0~9
        .Pattern = "[A-Za-z0-9]+"
把大小寫a~z,數字 0~9,用空白取代
        test = .Replace(s, "")
    End With
End Function
作者: jsleee    時間: 2024-3-31 07:49

回復 4# quickfixer


   quickfixer您好
     感謝您的指導,我好好研究研究,謝謝
   JS
作者: Andy2483    時間: 2024-4-1 10:10

謝謝二位前輩
後學藉此帖練習擷取出字串間的連續中文,請各位前輩指教

Option Explicit
Sub TEST()
Dim T$
T = "1100中文中文中文...AABBCC"
T = Replace(1 & T, Val(1 & T), "")
MsgBox T
T = Mid(T, 1, Evaluate("LENB(""" & T & """)") - Len(T))
MsgBox T
End Sub
作者: hcm19522    時間: 2024-4-2 10:27

(搜尋輸入編號12593) google網址:https://hcm19522.blogspot.com/




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)