Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a simple number pattern in VBA using RegEx [closed]

Tags:

regex

vba

How do I check if a string is either a one digit number OR a two digit number and otherwise return false?

like image 786
JOE SKEET Avatar asked Dec 01 '22 03:12

JOE SKEET


2 Answers

How about:

Function OneOrTwo(i As Integer) As Boolean
Dim objRegEx As Object
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True

objRegEx.Pattern = "^\d{1,2}$"

OneOrTwo = objRegEx.Test(i)
End Function

See: http://msdn.microsoft.com/en-us/library/ms974570.aspx

like image 167
Fionnuala Avatar answered Dec 05 '22 16:12

Fionnuala


You can also do this using VBA LIKE:

Function OneOrTwo(Digits As Variant) As Boolean
    OneOrTwo = Digits Like "#" Or Digits Like "##"
End Function
like image 21
Charles Williams Avatar answered Dec 05 '22 17:12

Charles Williams