Table Of Contents
What is RegEx in Excel VBA?
RegEx stands for "Regular Expression" in VBA Excel and is a sequence of characters that defines the search pattern for finding a specific pattern of characters in a string of values. So, in a simple word, we can create a regular expression pattern and use it to search for the string of that pattern.
VBA RegEx is an object model. We know it is intimidating to look at the explanation, but the thing is the nature of the object. First, you must remember that VBA RegEx (Regular Expression) is a text object like our other text functions, “LEFT, RIGHT, MID.”
How to Enable RegEx in Excel VBA?
As we told VBA, RegEx is an object model in VBA, just like our external software like "MS Word" and "MS PowerPoint." Similarly, RegEx is also a Component Object Model (COM), which we need to reference in the VBA editor. To enable RegEx, follow the below steps.
- Go to Visual Basic Editor (Alt + F11)
- Go to "Tools" and "References."
- Now, you will see references to the VBA Project. Scroll down and select "Microsoft VBScript Regular Expression 5.5."
- Now click on "OK." We can access this RegEx object now in VBA coding.
Example - Now, we will show you one simple example. Assume you have the words "Sales 2019, Sales 2018, and Sales 2017". If you define the pattern as , it matches all the numbers between 0 to 7. Therefore, our matches will be 201, 201, and 2017 in each string.
VBA RegEx Pattern
The pattern of the VBA RegEx function looks intimidating. It takes some time to understand the pattern. For example, we can see two kinds of sequences of characters: literal characters and metacharacters.
- Literal Characters search for the exact match of the provided string. For example, the literal character sequence "EFG" looks for all the matches of "EFG" in the provided text.
- Metacharacters are nothing but a combination of characters with exact meaning in the RegEx pattern. It is completely different from Literal Characters. It is a huge topic to cover. Below are some of the important syntaxes.
Syntax | Description | Example | Example Match |
. | It matches any single character of the input string. | p.t | Pet. Pot, Put, Pattern |
It matches any single character between the bracket of an input string. | It matches either p or t | ||
It matches any single character, not between the bracket of an input string. | It matches neither p or t | ||
It matches any character between the range provided in the bracket. | It matches any digit from 0 to 9 | ||
It matches any lower case character from a to z | |||
It matches any upper case character from A to Z | |||
s | It matches any white space character. | - | Matches Space, New Line, or Tab Character |
S | It matches any non-white-space character | - | Matches characters are not Space, not New Line, or not Tab Character |
d | It matches any single digit character. | SE 5 VG 6 | Matches 5 and 6 |
D | It matches any single non-digit character | SE 5 VG 6 | Matches SEVG |
Properties and Methods of RegEx Object
Like all our object models, RegEx, too, has its properties and methods. Now, we will see them one by one in detail.
Properties of VBA Regex Object
- Pattern: One may use it to match the provided string.
- Ignore Case: This is to ignore uppercase and lowercase characters.
- Global: If you want to find all matches in the pattern, then TRUE is the argument. Else, it will find the first match.
- Multi-Line: You can use this if you wish to find new line breaks.
Methods of RegEx Object
- Test: This is to test whether we can find the pattern in the provided string. It will return TRUE if found or else FALSE.
- Execute: This will return all the pattern matches against the finding string.
- Replace: This will replace the search string with the new string.
Example of RegEx in VBA Excel
Now, take a look at the below example VBA code.
Code:
Sub RegEx_Example() Dim RegEx As Object, MyString As String Set RegEx = CreateObject("VBScript.RegExp") With RegEx .Pattern = "+" End With MyString = "Date of Birth year is 1985" MsgBox RegEx.Test(MyString) MyString = "Date of Birth year is ???" MsgBox RegEx.Test(MyString) End Sub
regular
In the above code, we have set the pattern to search the number from 0 to 9.
With RegEx .Pattern = "+" End With
Then, the variable MyString = "Date of Birth year is 1985" holds the values from 0 to 9, so our message box will return TRUE.
MyString = "Date of Birth year is ???" doesn't have any numbers from 0 to 9, so it will return FALSE as the message box result.