I have a text box from which I need to extract certain numbers. The number will always be 7 digits, but the location inside the line is unknown and how many in the line is also unknown.
Example line: "SF WO 1564892 DUE 5/19 FIN WO 1638964 DUE 5/27". I want to be able to extract 1564892 and 1638964 and generate a new line like "1564892; 1638964" and continue to add "number" if there is more in the line. I use a new line to search and return the largest of these numbers.
I found this and it seems to work, but it will also return “1234567” from the string “123456789”, which is undesirable.
Public Function ExtractDigits(Alphanumeric As String, DigitLength As Long)
Dim StringLenght As Long
Dim CurrentCharacter As String
Dim NewString As String
Dim NumberCounter As Long
Dim TempString As String
StringLenght = Len(Alphanumeric)
For r = 1 To StringLenght
CurrentCharacter = Mid(Alphanumeric, r, 1)
If IsNumeric(CurrentCharacter) Then
NumberCounter = NumberCounter + 1
TempString = TempString & CurrentCharacter
If NumberCounter = DigitLength Then
If NewString = "" Then
NewString = TempString
Else
NewString = NewString & ";" & TempString
End If
End If
End If
If Not IsNumeric(CurrentCharacter) Then
NumberCounter = 0
TempString = ""
End If
Next
ExtractDigits = NewString
End Function
I would prefer the solution to be in VBA rather than in function, but I'm open to anything.