gpt4 book ai didi

vba - Excel VBA函数删除字符串末尾的字母

转载 作者:行者123 更新时间:2023-12-02 23:52:54 25 4
gpt4 key购买 nike

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"

因此应删除第一个数字之前的所有字母...

干杯,安迪

最佳答案

我测试了这个解决方案并且它有效。它总是查看最后一个 Char 并查看它是否是数字。如果不是,它会切断最后一个 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

关于vba - Excel VBA函数删除字符串末尾的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43818241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com