Updated on January 2, 2024
Article byJeevan A Y
Edited byAshish Kumar Srivastav
Reviewed byDheeraj Vaidya, CFA, FRM

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 functionsText FunctionsTEXT function in excel is a string function used to change a given input to the text provided in a specified number format. It is used when we large data sets from multiple users and the formats are different.read more, “LEFT, RIGHT, MID.”


You are free to use this image on your website, templates, etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA RegEx (wallstreetmojo.com)

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.

  1. Go to Visual Basic Editor (Alt + F11)

    vba regex 1

  2. Go to “Tools” and “References.”

    vba regex 1.1

  3. Now, you will see references to the VBA Project. Scroll down and select “Microsoft VBScript Regular Expression 5.5.”

    vba regex 1.2

  4. 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 [0 – 7], it matches all the numbers between 0 to 7. Therefore, our matches will be 201, 201, and 2017 in each string.

–>> If you want to learn Excel VBA professionally, then our VBA Basic Course (16+ hours) is the perfect solution. In our Basic Excel VBA course you learn the skill of automating tasks using Variables, Data Types, Ranges, and Cells in VBA. Master Control Structures, including Conditional Statements and Loops, and discover techniques for manipulating data through sorting, filtering, and formatting. By the end of the course, you will be able to apply your acquired skills to real projects, culminating in an Excel VBA project which will solidify your ability to create efficient, automated solutions.

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.
SyntaxDescriptionExampleExample Match
.It matches any single character of the input string.p.tPet. Pot, Put, Pattern
[]It matches any single character between the bracket of an input string.[pt]It matches either p or t
[^]It matches any single character, not between the bracket of an input string.[^pt]It matches neither p or t
[First-Last]It matches any character between the range provided in the bracket.[0-9]It matches any digit from 0 to 9
 [a-z] It matches any lower case character from a to z
 [A-Z] It matches any upper case character from A to Z
sIt matches any white space character.Matches Space, New Line, or Tab Character
SIt matches any non-white-space characterMatches characters are not Space, not New Line, or not Tab Character
dIt matches any single digit character.SE 5 VG 6Matches 5 and 6
DIt matches any single non-digit characterSE 5 VG 6Matches 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

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 codeExample VBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more.

You can download this VBA RegEx Excel Template here – VBA RegEx Excel Template


Sub RegEx_Example()

    Dim RegEx As Object, MyString As String
    Set RegEx = CreateObject("VBScript.RegExp")

    With RegEx
      .Pattern = "[0-9]+"
    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


In the above code, we have set the pattern to search the number from 0 to 9.

With RegEx

  .Pattern = "[0-9]+"

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.

example Gif

Recommended Articles

This article has been a guide to VBA RegEx. Here, we discuss using RegEx (Regular expression) in Excel VBA along with its properties, pattern, and example. You can learn more about Excel VBA from the following articles: –