Tuesday, July 19, 2016

Find Using Regular Expression in Excel - New Fuction

I've found that regular expression is one of the most useful tools in data manipulation. Being able to pass a regular expression rule and return the first instance of that expression is a powerful function.    I'm not going to get into depth on how regular expression works, there are tons of great resources on line that can help you learn how to write the expressions.    I might to into some depth later on other tools I use online, but here I'm just going to give you the VBA function that will expose the power of regular expression into your excel spreadsheet.  

Here is the code:
'-----------------------------------------------FRE---------------------------------------
Public Function FRE(StringIn As String, strPattern As String) As String
  Dim r As RegExp
    Set r = New RegExp
      With r
            .Global = True
            .MultiLine = False
            .IgnoreCase = True
            .Pattern = strPattern
        End With
   Set m = r.Execute(StringIn)
   FRE = Trim(m(0))
End Function
'------------------------------------------------------------------------------------------------

I named this function FRE - simply because I use it a ton, and didn't want to type some totally expressive name, - FRE is Find using Regular Expression.

There are two parts - the Stringin - which is the string your searching in, and strPattern which is the regular Expression Pattern.   If this function finds anything, it's only going to return the first instance.   I'm sure we can write better functions for returning arrays, making ranges, etc.   This is just dirt simple. 

Here is an example of how you could use this function -  you have a cell that has a lot of text mixed with phone numbers, but you just want to return the phone numbers from that cell, this function will return the first phone number.

Lets Say A1 = "Joe Smith (456) 456-8456 josmith@google.com"
to return the phone number, the function would look like this FRE(A1,"(\(\d{3}\).*\d{3}.*\d{4})")

And the return would be - (456) 456-8456


Super Simple.    I hope this will help somebody out there!












No comments:

Post a Comment