2010-08-23 15 views

risposta

8

Qualcosa del genere dovrebbe fare il trucco per voi:

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 

È quindi possibile chiamare in questo modo:

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

È possibile semplificare la funzione. Non hai davvero bisogno di 'currentCharacter'; il tuo test potrebbe essere 'If IsNumeric (Mid (s, i, 1)) Then ...' – e100

+0

@ e100 - molto vero. Rompere rende più leggibile e questo è quello che sono per rispondere a una domanda di questo livello di complessità mentre lavoro sulla base del fatto che l'OP potrebbe richiedere un codice più semplice/più chiaro per aiutare a capire =) – Rob

0

Se la velocità è un problema, questo verrà eseguito un po 'più veloce di 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 
1

Si potrebbe provare regex, e quindi avresti due problemi. Il mio VBAfu non è fino al tabacco da fiuto, ma ho deciso di dargli un andare:

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 

Poi basta chiamano con FirstDigit("ololo123").

+0

+1 Vedo che avevamo simili pensieri, eri un po 'più veloce! –

2
Non

sicuro al proprio ambiente, ma questo ha funzionato 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 
2

Io in realtà sono quella funzione:

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 
7

Ecco un metodo leggero e veloce che evita le aggiunte regex/di riferimento, quindi aiutare con spese generali e di trasporto dovrebbe essere un vantaggio.

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 
Problemi correlati