Quantcast
Viewing latest article 2
Browse Latest Browse All 4

Using Regular Expressions for sub strings in Excel

Ever wanted to extract a sub string from a cell in Microsoft Excel using a Regular expression? Here’s a Visual Basic snippet that will do just that.

Function getRegexGroup(inRegex As String, inValue As String, inGroup As Integer)
    Set myRegExp = CreateObject("vbscript.regexp")
    myRegExp.IgnoreCase = True
    myRegExp.Global = True
    myRegExp.Pattern = inRegex
    Set myMatches = myRegExp.Execute(inValue)
    Set SubMatches = myMatches.Item(0).SubMatches
 
    getRegexGroup = SubMatches.Item(inGroup)
End Function

You can use it like so:

Image may be NSFW.
Clik here to view.
Extract Regular Expression Group in Excel

=getRegexGroup([REGULAR EXPRESSION], [INPUT STRING], [GROUP NUMBER TO EXTRACT])

Viewing latest article 2
Browse Latest Browse All 4

Trending Articles