VBA. How to find position of first digit in string

前端 未结 6 1249
陌清茗
陌清茗 2020-12-31 08:47

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

6条回答
  •  星月不相逢
    2020-12-31 09:27

    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
    

提交回复
热议问题