I'm trying to fix some code I have in VBA to get the number after my string.
For example I have a column (Y) where has the following data sets
"VM" is my string for the Prefix and I'd like to extract the number after "VM" so that getFirstDigit = 1 (in this example).
Right now however it keeps pulling in 2 from S25 in my PrefixString.
I don't want to just look for the last digit cause the data set can change the position but the Prefix# will always remain the same. For example it might be OBC VM1 S25.
Code:
Public Function getFirstDigit(PrefixString As String) As Integer
Dim prefix As String
prefix = ActiveSheet.Range("B2").Value
With CreateObject("VBScript.RegExp")
PrefixString = Replace(PrefixString, "" & prefix & "", "")
.Pattern = "^\D*(\d)"
.Global = False
If .test(PrefixString) Then
getFirstDigit = .Execute(PrefixString)(0).SubMatches(0)
Else
getFirstDigit = "1"
End If
End With
End Function
Any help would be greatly appreciated! -D

PrefixString = Replace(PrefixString, "" & prefix & "", "")for ? Wouldn't.Pattern = prefix & "(\d+)"do it ?