VBA。如何找到字符串中第一个数字的位置

发布于 2024-09-15 23:11:53 字数 52 浏览 4 评论 0原文

我有字符串“ololo123”。 我需要获取第一个数字的位置 - 1。 如何设置搜索掩码?

I have string "ololo123".
I need get position of first digit - 1.
How to set mask of search ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

守护在此方 2024-09-22 23:11:53

这是一种轻量级且快速的方法,可以避免添加正则表达式/引用,从而有助于减少开销和可移植性(这应该是一个优势)。

Public Function GetNumLoc(xValue As String) As Integer

For GetNumLoc = 1 To Len(xValue)
    If Mid(xValue, GetNumLoc, 1) Like "#" Then Exit Function
Next

GetNumLoc = 0

End Function

Here is a lightweight and fast method that avoids regex/reference additions, thus helping with overhead and transportability should that be an advantage.

Public Function GetNumLoc(xValue As String) As Integer

For GetNumLoc = 1 To Len(xValue)
    If Mid(xValue, GetNumLoc, 1) Like "#" Then Exit Function
Next

GetNumLoc = 0

End Function
感悟人生的甜 2024-09-22 23:11:53

像这样的东西应该适合你:

Public Function GetPositionOfFirstNumericCharacter(ByVal s As String) As Integer
    For i = 1 To Len(s)
        Dim currentCharacter As String
        currentCharacter = Mid(s, i, 1)
        If IsNumeric(currentCharacter) = True Then
            GetPositionOfFirstNumericCharacter = i
            Exit Function
        End If
    Next i
End Function

然后你可以这样称呼它:

Dim iPosition as Integer
iPosition = GetPositionOfFirstNumericCharacter("ololo123")

Something like this should do the trick for you:

Public Function GetPositionOfFirstNumericCharacter(ByVal s As String) As Integer
    For i = 1 To Len(s)
        Dim currentCharacter As String
        currentCharacter = Mid(s, i, 1)
        If IsNumeric(currentCharacter) = True Then
            GetPositionOfFirstNumericCharacter = i
            Exit Function
        End If
    Next i
End Function

You can then call it like this:

Dim iPosition as Integer
iPosition = GetPositionOfFirstNumericCharacter("ololo123")
氛圍 2024-09-22 23:11:53

不确定您的环境,但这在 Excel 2010 中有效

'Added reference for Microsoft VBScript Regular Expressions 5.5

Const myString As String = "ololo123"
Dim regex As New RegExp
Dim regmatch As MatchCollection

regex.Pattern = "\d"
Set regmatch = regex.Execute(myString)
MsgBox (regmatch.Item(0).FirstIndex)   ' Outputs 5

Not sure on your environment, but this worked in Excel 2010

'Added reference for Microsoft VBScript Regular Expressions 5.5

Const myString As String = "ololo123"
Dim regex As New RegExp
Dim regmatch As MatchCollection

regex.Pattern = "\d"
Set regmatch = regex.Execute(myString)
MsgBox (regmatch.Item(0).FirstIndex)   ' Outputs 5
滥情稳全场 2024-09-22 23:11:53

我实际上有这个功能:

Public Function GetNumericPosition(ByVal s As String) As Integer
    Dim result As Integer
    Dim i As Integer
    Dim ii As Integer

    result = -1
    ii = Len(s)
    For i = 1 To ii
        If IsNumeric(Mid$(s, i, 1)) Then
            result = i
            Exit For
        End If
    Next
    GetNumericPosition = result
End Function

I actually have that function:

Public Function GetNumericPosition(ByVal s As String) As Integer
    Dim result As Integer
    Dim i As Integer
    Dim ii As Integer

    result = -1
    ii = Len(s)
    For i = 1 To ii
        If IsNumeric(Mid$(s, i, 1)) Then
            result = i
            Exit For
        End If
    Next
    GetNumericPosition = result
End Function
甜嗑 2024-09-22 23:11:53

您可以尝试正则表达式,然后您会遇到两个问题。我的 VBAfu 不合格,但我会尝试一下:

Function FirstDigit(strData As String) As Integer
    Dim RE As Object REMatches As Object

    Set RE = CreateObject("vbscript.regexp")
    With RE
        .Pattern = "[0-9]"
    End With

    Set REMatches = RE.Execute(strData)
    FirstDigit = REMatches(0).FirstIndex
End Function

然后您只需使用 FirstDigit("ololo123") 调用它即可。

You could try regex, and then you'd have two problems. My VBAfu is not up to snuff, but I'll give it a go:

Function FirstDigit(strData As String) As Integer
    Dim RE As Object REMatches As Object

    Set RE = CreateObject("vbscript.regexp")
    With RE
        .Pattern = "[0-9]"
    End With

    Set REMatches = RE.Execute(strData)
    FirstDigit = REMatches(0).FirstIndex
End Function

Then you just call it with FirstDigit("ololo123").

夜未央樱花落 2024-09-22 23:11:53

如果速度是一个问题,这将比 Robs (noi Rob) 运行得快一些:

Public Sub Example()
    Const myString As String = "ololo123"
    Dim position As Long
    position = GetFirstNumeric(myString)
    If position > 0 Then
        MsgBox "Found numeric at postion " & position & "."
    Else
        MsgBox "Numeric not found."
    End If
End Sub

Public Function GetFirstNumeric(ByVal value As String) As Long
    Dim i As Long
    Dim bytValue() As Byte
    Dim lngRtnVal As Long
    bytValue = value
    For i = 0 To UBound(bytValue) Step 2
        Select Case bytValue(i)
            Case vbKey0 To vbKey9
                If bytValue(i + 1) = 0 Then
                    lngRtnVal = (i \ 2) + 1
                    Exit For
                End If
        End Select
    Next
    GetFirstNumeric = lngRtnVal
End Function

If speed is an issue, this will run a bit faster than Robs (noi Rob):

Public Sub Example()
    Const myString As String = "ololo123"
    Dim position As Long
    position = GetFirstNumeric(myString)
    If position > 0 Then
        MsgBox "Found numeric at postion " & position & "."
    Else
        MsgBox "Numeric not found."
    End If
End Sub

Public Function GetFirstNumeric(ByVal value As String) As Long
    Dim i As Long
    Dim bytValue() As Byte
    Dim lngRtnVal As Long
    bytValue = value
    For i = 0 To UBound(bytValue) Step 2
        Select Case bytValue(i)
            Case vbKey0 To vbKey9
                If bytValue(i + 1) = 0 Then
                    lngRtnVal = (i \ 2) + 1
                    Exit For
                End If
        End Select
    Next
    GetFirstNumeric = lngRtnVal
End Function
那些过往 2024-09-22 23:11:53

spere 答案的改进版本(无法编辑他的答案),适用于任何模式

Private Function GetNumLoc(textValue As String, pattern As String) As Integer
    For GetNumLoc = 1 To (Len(textValue) - Len(pattern) + 1)
        If Mid(textValue, GetNumLoc, Len(pattern)) Like pattern Then Exit Function
    Next

    GetNumLoc = 0
End Function

获取模式值,您可以使用以下内容:

Private Function GetTextByPattern(textValue As String, pattern As String) As String
    Dim NumLoc As Integer
    For NumLoc = 1 To (Len(textValue) - Len(pattern) + 1)
        If Mid(textValue, NumLoc, Len(pattern)) Like pattern Then
            GetTextByPattern = Mid(textValue, NumLoc, Len(pattern))
            Exit Function
        End If
    Next    

    GetTextByPattern = ""
End Function

示例使用:

dim bill as String
bill = "BILLNUMBER 2202/1132/1 PT2200136"

Debug.Print GetNumLoc(bill , "PT#######")
'Printed result:
'24

Debug.Print GetTextByPattern(bill , "PT#######")
'Printed result:
'PT2200136

An improved version of spere's answer (can't edit his answer), which works for any pattern

Private Function GetNumLoc(textValue As String, pattern As String) As Integer
    For GetNumLoc = 1 To (Len(textValue) - Len(pattern) + 1)
        If Mid(textValue, GetNumLoc, Len(pattern)) Like pattern Then Exit Function
    Next

    GetNumLoc = 0
End Function

To get the pattern value you can use this:

Private Function GetTextByPattern(textValue As String, pattern As String) As String
    Dim NumLoc As Integer
    For NumLoc = 1 To (Len(textValue) - Len(pattern) + 1)
        If Mid(textValue, NumLoc, Len(pattern)) Like pattern Then
            GetTextByPattern = Mid(textValue, NumLoc, Len(pattern))
            Exit Function
        End If
    Next    

    GetTextByPattern = ""
End Function

Example use:

dim bill as String
bill = "BILLNUMBER 2202/1132/1 PT2200136"

Debug.Print GetNumLoc(bill , "PT#######")
'Printed result:
'24

Debug.Print GetTextByPattern(bill , "PT#######")
'Printed result:
'PT2200136
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文