My requirement is that I need to check whether a string variable has first character "P" followed by digits.
For example
P0
P123
P22
To-date I have looked at options for using Like as per here.
You can also use the native Like operator:
Sub Foo()
Dim x As String, matches As Boolean
x = "P123"
matches = (x Like "P" & Application.Rept("[0-9]", Len(x) - 1))
Debug.Print matches
End Sub
You could use a simple RegExp:
Function strOut(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "^P\d+$"
strOut = .Test(strIn)
End With
End Function
test code
Sub Test()
Debug.Print strOut("P22")
Debug.Print strOut("aP22")
Debug.Print strOut("P12344")
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With