VBA专家,
我正在寻找一个能从字符串变量中删除最后一个字母[A-Z]的函数。
例如:
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"
因此,应删除第一个号码以外的所有字母......
干杯, 安迪
答案 0 :(得分:2)
您正在寻找以下解决方案:
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
答案 1 :(得分:2)
您可以使用正则表达式:
模式(\w+\d+)(\w+)
表示匹配2个组。第一组是一些字母,后面是一些数字。第二组是一些字母。然后Replace
函数说只用第一组替换原始字符串,忽略第二组。这只留下了第一组一些字母和一些数字。
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
答案 2 :(得分:2)
另一种方法:
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
答案 3 :(得分:1)
或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
答案 4 :(得分:1)
我测试了这个解决方案并且它有效。它看起来总是在最后的Char,看起来它是一个数字还是不是。如果没有,它会切断最后一个字符,依此类推。
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