String Excel VBA循环遍历一组数字,直到找到一个字母

String Excel VBA循环遍历一组数字,直到找到一个字母,string,vba,excel,String,Vba,Excel,我在一个单元格中有一个字符串,假设它是“Client Ref:F123456PassPlus”。 可能字符串的数字前没有字母,可能数字中有符号,可能字母和数字之间有空格。 我只需要将数字提取为变量。我有代码来做,但它不知道什么时候停止循环字符串。当有数字或符号以外的东西时,它应该停止,但它会继续 IsNumber = 1 ref = "" If branch = "" Then e = b Else e = b + 1 End If f = 1 While IsNumber =

我在一个单元格中有一个字符串,假设它是“Client Ref:F123456PassPlus”。 可能字符串的数字前没有字母,可能数字中有符号,可能字母和数字之间有空格。 我只需要将数字提取为变量。我有代码来做,但它不知道什么时候停止循环字符串。当有数字或符号以外的东西时,它应该停止,但它会继续

IsNumber = 1
ref = ""
If branch = "" Then
    e = b
Else
    e = b + 1
End If
f = 1
While IsNumber = 1
    For intpos = 1 To 15
        ref = Mid(x, e, f)
        f = f + 1
        Select Case Asc(ref)
            Case 45 To 57
                IsNumber = 1
            Case Else
                IsNumber = 0
                Exit For
        End Select
    Next
    IsNumber = 0
Wend

任何没有定义的变量字母之前都已定义,e告诉代码从何处开始复制,x是包含字符串的单元格。现在,一切都很好,它从数字开始复制它们,并将它们构建成一个越来越大的字符串,但它只有在intpos达到15时才会停止。

此代码松散地基于您的,工作(生成“12345”)。对于大字符串或更复杂的提取需求,我将考虑学习正则表达式COM对象。

Function ExtractNumber(ByVal text As String) As String

  ExtractNumber = ""

  foundnumber = False

  For e = 1 To Len(text)
    ref = Mid(text, e, 1)
        Select Case Asc(ref)
            Case 45 To 57   'this includes - . and /, if you want only digits, should be 48 to 57
                foundnumber = True
                ExtractNumber = ExtractNumber & ref
            Case Else
                If foundnumber = True Then Exit For
        End Select
  Next

End Function

您尝试完成此任务的方式没有问题,但我还是忍不住建议使用正则表达式:-)

此示例将从
A1
中的字符串中删除所有非数字,并将结果显示在消息框中。使用的模式是
[^0-9]

Sub StripDigits()
    Dim strPattern As String: strPattern = "[^0-9]"
    Dim strReplace As String: strReplace = vbnullstring
    Dim regEx As New RegExp
    Dim strInput As String
    Dim Myrange As Range

    Set Myrange = ActiveSheet.Range("A1")

    If strPattern <> "" Then
        strInput = Myrange.Value
        strReplace = ""

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.test(strInput) Then
            MsgBox (regEx.Replace(strInput, strReplace))
        Else
            MsgBox ("Not matched")
        End If
    End If
End Sub
Sub-StripDigits()
将strPattern设置为字符串:strPattern=“[^0-9]”
作为字符串的Dim strReplace:strReplace=vbnullstring
Dim regEx作为新的RegExp
像弦一样的模糊的条纹
将Myrange变暗为Range
设置Myrange=ActiveSheet.Range(“A1”)
如果strPattern“”则
strInput=Myrange.Value
strReplace=“”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
MsgBox(正则表达式替换(strInput,strReplace))
其他的
MsgBox(“不匹配”)
如果结束
如果结束
端接头
确保添加对“Microsoft VBScript正则表达式5.5”的引用

有关如何在Excel中使用正则表达式的详细信息,包括循环范围的示例

结果:


我去掉了Asc检查,并在构建数字“字符串”之前对每个字符进行检查


无论您想在何处结束sub,只需添加
Exit sub
。我需要宏知道它什么时候有字母在里面(或者最好就在前面),然后我会在代码中使用
Exit For
。根据我所写的,我认为它应该这样做,但显然不是。您是否调试了.print Asc(ref)以查看值?目前,对于一个5个字符长的数字,ref的值为“12345passplus::”,这是因为它一直添加到ref变量,直到intpos达到15。很好,我正在考虑使用regex,但我对它的了解还不足以写任何东西。好主意!好办法。使用
vbnullstring
IsNumber = 1
ref = ""
If branch = "" Then
    e = b
Else
    e = b + 1
End If
f = 1
While IsNumber = 1
    For intpos = 1 To 15
        char = Mid(x, e + intpos, 1)
        f = f + 1
        If IsNumeric(char) Then
            ref = Mid(x, e, f)
            IsNumber = 1
        Else
            IsNumber = 0
            Exit For
        End If
    Next
    IsNumber = 0
Wend