How to map a prime number pattern in VBA using RegEx

How to check if a string is either a single-digit number or a two-digit number, and otherwise returns false ?

+4
source share
5 answers

What 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

+8
source

You can also do this using VBA LIKE:

 Function OneOrTwo(Digits As Variant) As Boolean OneOrTwo = Digits Like "#" Or Digits Like "##" End Function 
+7
source
 IF CInt(myNumberString) < 100 Then MsgBox "String must be either 1 or 2 digits" Else Msgbox "Failed" End IF 

Should work for you.

+1
source

Remou had it right. Here is the RegexContains function, so you can use it with all types of templates, not just the ones you need now.

 Function RegexContains(ByVal find_in As String, _ ByVal find_what As String, _ Optional IgnoreCase As Boolean = False) As Boolean Dim RE As Object Set RE = CreateObject("vbscript.regexp") RE.Pattern = find_what RE.IgnoreCase = IgnoreCase RE.Global = True RegexContains = RE.Test(find_in) End Function 

Following the example from Remou, counting cell A1, you should write:

= RegexContains (A1, "^ \ d {1,2} $")

+1
source

Here is what I would like to try. / d for digits ,? for options 2 digits.

/ d / d?

or

/ d {1,2}

0
source

Source: https://habr.com/ru/post/1337205/


All Articles