Solved

Regular expression in vba for excel

Posted on 2010-08-17
7
252 Views
Last Modified: 2012-05-10
I looking stumped on how to write the regexp for this word pattern "Node XXX 200Z"  where "x" could be 1-999  and "z" could be any number 1-9
0
Comment
Question by:rpelfrey
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:javaftper
ID: 33456927
I have written a sub to call a function which returns 'No match' if the pattern fails or the node name if a match is found-

Sub TestText()
    Const strTest As String = "Node 769 2009" 'test value
    MsgBox REGEXTEST(strTest)
End Sub

Function REGEXTEST(strData As String) As String
    Dim RE As Object, REMatches As Object

    Set RE = CreateObject("vbscript.regexp")
    With RE
        .MultiLine = False
        .Global = False
        '.IgnoreCase = True if necessary you can ignore case
        .Pattern = "(Node )[1-9][1-9][1-9]( )(200[1-9])"
    End With
   
    Set REMatches = RE.Execute(strData)
    If REMatches.Count = 0 Then
    REGEXTEST = "No match"
    Else
    REGEXTEST = REMatches(0)
    End If
   

End Function
0
 
LVL 81

Expert Comment

by:byundt
ID: 33457189
Assuming that the pattern ought to allow for "Node 900 2008", I modified javaftper's code with the following statement:
 .Pattern = "Node\s([1-9]\d{2})\s200([1-9])"
0
 
LVL 81

Expert Comment

by:byundt
ID: 33457215
My pattern put the xxx and y parts of the pattern within parentheses so you can retrieve the submatches separately. With reference to javaftper's Else statement:
    REGEXTEST = REMatches(0).submatches(0) & " " & REMatches(0).submatches(1)   'Retrieve xxx and y separately
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rpelfrey
ID: 33457435
I am getting "No Match" see attached

samplenode.xls
0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 33457656
Change the pattern as follows:
.Pattern = "Node\s+([1-9]\d{0,2})\s+200([1-9])"

All nodes will match, even those with more than one space. If you require exactly one space, then remove the two + from the pattern.
0
 

Author Comment

by:rpelfrey
ID: 33457941
That did it.  I was using a vb script  that I had found that allows you to define the pattern while implementing the search/function (=RegExpFind(B1,"Node\s+([1-9]\d{0,2})\s+200(9)").  

It was written by matthewspatrick at this site http://www.vbaexpress.com.  I just could not defined the pattern properly.
0
 

Author Closing Comment

by:rpelfrey
ID: 33457950
Great Job!!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to combine three formulas into one 5 40
Increment default InPutBox value 14 26
Formula returning #N/A 9 35
Excel formula that extracts out name of url 6 46
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question