Excel VBA函数在字符串末尾删除字母 [英] Excel VBA function to remove letters at end of string

查看:1730
本文介绍了Excel VBA函数在字符串末尾删除字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBA专家,

我正在寻找一个功能,将从字符串变量中删除最后一个字母[AZ]。

I'm looking to build a function which will remove last letters [A-Z] from a string variable.

例如:

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"

所以所有的字母应该删除第一个数字...

So all the letters up to first number should be removed...

干杯,
Andy

Cheers, Andy

推荐答案

我测试这个解决方案,它的工作原理。它总是在最后一个Char,看看是否是一个数字。如果没有,它会切断最后一个Char等等。

I Testet this solution and it works. It looks always at the last Char and looks if it is a number or Not. If not it cut off the last Char and so on.

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

这篇关于Excel VBA函数在字符串末尾删除字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆