Excel VBA RegEx formula

wheelsguy

New Member
Joined
Sep 23, 2013
Messages
9
Good morning.

I am new to Regular Expressions, and I am trying to validate a number like 012345-1234. I have the following:

Code:
Public Function ValidNumber(ByVal strNumber As String) As Boolean
    With CreateObject("VBScript.RegExp")
        .IgnoreCase = True
        .Global = True
        .Pattern = "^([0-9][0-9][0-9][0-9][0-9][0-9])-([0-9][0-9][0-9][0-9])$"
        ValidNumber = .Test(strNumber)
    End With
End Function

My code works, but I was hoping there was a better (shorter experession) way where it could be more recursive.

Thank you for looking, WHEELS
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi

I'm glad your problem is solved.

Since the purpose of the udf is just to test that pattern, I'd just add some remarks:

1 -
Code:
        .IgnoreCase = True

In this case your pattern does not have letters so you can just ignore this property

2 -
Code:
        .Global = True

In this case this property doesn't matter, so ignore it

3 -

Code:
"^([0-9]{6})-([0-9]{4})$"

You don't need the parentheses, and, like Jim said, you can use "\d" for a digit.

Conclusion:

You can try a simplified version of the code:

Code:
Public Function ValidNumber(ByVal strNumber As String) As Boolean
    With CreateObject("VBScript.RegExp")
        .Pattern = "^\d{6}-\d{4}$"
        ValidNumber = .Test(strNumber)
    End With
End Function


Last remark: the code could still be made more efficient if you use this udf a lot in the worksheet, by making the object persistent.
 
Upvote 0
Hi

I'm glad your problem is solved.

Since the purpose of the udf is just to test that pattern, I'd just add some remarks:

1 -
Code:
        .IgnoreCase = True

In this case your pattern does not have letters so you can just ignore this property

2 -
Code:
        .Global = True

In this case this property doesn't matter, so ignore it

3 -

Code:
"^([0-9]{6})-([0-9]{4})$"

You don't need the parentheses, and, like Jim said, you can use "\d" for a digit.

Conclusion:

You can try a simplified version of the code:

Code:
Public Function ValidNumber(ByVal strNumber As String) As Boolean
    With CreateObject("VBScript.RegExp")
        .Pattern = "^\d{6}-\d{4}$"
        ValidNumber = .Test(strNumber)
    End With
End Function


Last remark: the code could still be made more efficient if you use this udf a lot in the worksheet, by making the object persistent.

Great suggestions. Thank you pgc01</SPAN></SPAN>.
 
Upvote 0
Good morning.

I am new to Regular Expressions, and I am trying to validate a number like 012345-1234. I have the following:

Code:
Public Function ValidNumber(ByVal strNumber As String) As Boolean
    With CreateObject("VBScript.RegExp")
        .IgnoreCase = True
        .Global = True
        .Pattern = "^([0-9][0-9][0-9][0-9][0-9][0-9])-([0-9][0-9][0-9][0-9])$"
        ValidNumber = .Test(strNumber)
    End With
End Function

My code works, but I was hoping there was a better (shorter experession) way where it could be more recursive.
Are you aware you can do this without resorting to using a Regular Expression engine...

Code:
Function ValidNumber(ByVal strNumber As String) As Boolean
  ValidNumber = strNumber Like "######-####"
End Function

And, because the function is nothing more than a simple one-liner, you really do not need the function housing at all... you can simply use the right side of the code line in an If..Then test directly within your own code..
 
Last edited:
Upvote 0
Are you aware you can do this without resorting to using a Regular Expression engine...

Code:
Function ValidNumber(ByVal strNumber As String) As Boolean
  ValidNumber = strNumber Like "######-####"
End Function

And, because the function is nothing more than a simple one-liner, you really do not need the function housing at all... you can simply use the right side of the code line in an If..Then test directly within your own code..

Thanks Rick. I haven't had a chance to test it yet, but it looks pretty clean and powerful. It resembles SQL (slightly). What would this techinque be called, and how would it be utilized to replace Regular Expressions for more complex validations?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top