Excel VBA函数删除string末尾的字母

VBA专家,

我正在build立一个函数,将从stringvariables中删除最后一个字母[AZ]。

例如:

Sub Example() Dim MyString() as string ... ReDim Preserve MyString(3) MyString(1) = "ABC345A" MyString(2) = "DEFG6789BC" MyString(3) = "AHIL2431LTR" MyString(1) = RemLetters(MyString(1)) MyString(2) = RemLetters(MyString(2)) MyString(3) = RemLetters(MyString(3)) ... ... End Sub Function RemLetters(MyString) ??? End Function 

我期待函数返回:

 MyString(1) = "ABC345" MyString(2) = "DEFG6789" MyString(3) = "AHIL2431" 

所以,所有的字母到第一个数字应该被删除…

干杯,安迪

我Testet这个解决scheme,它的工作原理。 它总是看最后一个字符,看看它是否是一个数字。 如果没有,则切断最后一个字符,等等。

 Sub Example() Dim MyString() As String ReDim Preserve MyString(3) MyString(1) = "ABC345A" MyString(2) = "DEFG6789BC" MyString(3) = "AHIL2431LTR" MyString(1) = RemLetters(MyString(1)) MyString(2) = RemLetters(MyString(2)) MyString(3) = RemLetters(MyString(3)) End Sub Function RemLetters(MyString As String) As String Dim bolExit As Boolean bolExit = True Do While bolExit = True Select Case Asc(Right$(MyString, 1)) Case 65 To 90, 97 To 122 'IsLetter = True MyString = Left$(MyString, Len(MyString) - 1) Case Else 'IsLetter = False bolExit = False End Select Loop RemLetters = MyString End Function 

以下解决scheme可能是您正在寻找的:

 Option Explicit Function RemLetters(MyString As String) As String Dim i As Long MyString = StrReverse(MyString) For i = 1 To Len(MyString) If Asc(Mid(MyString, i, 1)) >= 48 And _ Asc(Mid(MyString, i, 1)) <= 57 Then Exit For Next i RemLetters = Mid(StrReverse(MyString), 1, Len(MyString) - i + 1) End Function 

你可以使用正则expression式:

模式(\w+\d+)(\w+)表示匹配2组。 第一组是一些字母,然后是一些数字。 第二组是一些字母。 然后, Replace函数说,用第一组replace原来的string,忽略第二组。 这给你留下了第一批一些字母和一些数字。

 Option Explicit Sub Example() Dim MyString() As String ReDim Preserve MyString(3) MyString(1) = "ABC345A" MyString(2) = "DEFG6789BC" MyString(3) = "AHIL2431LTR" MyString(1) = RemLetters(MyString(1)) MyString(2) = RemLetters(MyString(2)) MyString(3) = RemLetters(MyString(3)) Debug.Print MyString(1) Debug.Print MyString(2) Debug.Print MyString(3) End Sub Function RemLetters(MyString) As String Dim objRegex As Object Dim strOut As String Set objRegex = CreateObject("VBScript.Regexp") With objRegex .Pattern = "(\w+\d+)(\w+)" .Global = True strOut = .Replace(MyString, "$1") End With RemLetters = strOut End Function 

另一种方法:

 Public Function RemoveCharFromString(ByVal stringValue As String) As String Dim idx As Long, charCounter As Long For idx = Len(stringValue) To 1 Step -1 If IsNumeric(Mid(stringValue, idx, 1)) Then Exit For charCounter = charCounter + 1 Next idx RemoveCharFromString = Left(stringValue, Len(stringValue) - charCounter) End Function 

或RegEx可以像这样使用…

 Function RemLetters(MyString) As String Dim objRegex As Object Dim strOut As String Set objRegex = CreateObject("VBScript.Regexp") With objRegex .Pattern = "\w+\d+" .Global = False If .test(MyString) Then strOut = .Execute(MyString)(0) End With RemLetters = strOut End Function Sub Example() Dim MyString() As String ReDim Preserve MyString(3) MyString(1) = "ABC345A" MyString(2) = "DEFG6789BC" MyString(3) = "AHIL2431LTR" MyString(1) = RemLetters(MyString(1)) MyString(2) = RemLetters(MyString(2)) MyString(3) = RemLetters(MyString(3)) Debug.Print MyString(1) Debug.Print MyString(2) Debug.Print MyString(3) End Sub