从字母数字string中检索字母字符

我怎样才能用excel-vba分割AB2468123

我沿着这些线路尝试了一些东西

 myStr = "AB2468123" split(myStr, "1" OR "2" OR "3"......."9") 

我只想得到字母(字母)。

谢谢。

怎么样才能从一个inputstring检索只有字母:

 Function GetLettersOnly(str As String) As String Dim i As Long, letters As String, letter As String letters = vbNullString For i = 1 To Len(str) letter = VBA.Mid$(str, i, 1) If Asc(LCase(letter)) >= 97 And Asc(LCase(letter)) <= 122 Then letters = letters + letter End If Next GetLettersOnly = letters End Function Sub Test() Debug.Print GetLettersOnly("abc123") // prints "abc" Debug.Print GetLettersOnly("ABC123") // prints "ABC" Debug.Print GetLettersOnly("123") // prints nothing Debug.Print GetLettersOnly("abc123def") // prints "abcdef" End Sub 

编辑:为了完整性(和克里斯Neilsen)这里是正则Regex方式:

 Function GetLettersOnly(str As String) As String Dim result As String, objRegEx As Object, match As Object Set objRegEx = CreateObject("vbscript.regexp") objRegEx.Pattern = "[a-zA-Z]+" objRegEx.Global = True objRegEx.IgnoreCase = True If objRegEx.test(str) Then Set match = objRegEx.Execute(str) GetLettersOnly = match(0) End If End Function Sub test() Debug.Print GetLettersOnly("abc123") //prints "abc" End Sub 

这是我发现,最好的作品。 这可能有点基本,但它的工作:)

  Function Split_String(Optional test As String = "ABC111111") As Variant For i = 1 To Len(test) letter = Mid(test, i, 1) If IsNumeric(letter) = True Then justletters = Left(test, i - 1) justnumbers = Right(test, Len(test) - (i - 1)) Exit For End If Next 'MsgBox (justnumbers) 'MsgBox (justletters) 'just comment away the answer you want to have :) 'Split_String = justnumbers 'Split_String = justletters End Function 

更简单的单注RegExp

 Sub TestIt() MsgBox CleanStr("AB2468123") End Sub Function CleanStr(strIn As String) As String Dim objRegex As Object Set objRegex = CreateObject("vbscript.regexp") With objRegex .Pattern = "[^a-zA-Z]+" .Global = True CleanStr = .Replace(strIn, vbNullString) End With End Function