Home » excel » Search the VBA source code with RegEx

Search the VBA source code with RegEx

Posted by: admin April 23, 2020 Leave a comment


I need to find all the occurrences of a particular RegEx in my source code (i.e. col*r ). I realized you can programmatically search through your code for patterns (RegEx) if you use the VBComponents.CodeModule.Find() method as it’s explained in here and here. But that does not meet my needs as it only tells you whether such expression is found or not. I need the actual expression found in the module as well (e.g. colour and color).

Is there any way to achieve this programmatically within VBA?

How to&Answers:
Dim re, match
Set re = CreateObject("vbscript.regexp")
re.Pattern = "your regex"
re.Global = True

For Each match In re.Execute("you input")
    MsgBox match.Value

for more information check this link:http://msdn.microsoft.com/en-us/library/ms974570.aspx