Simple RegEx Sub and/or Function

I'm looking for some code that will determine whether a cell in a targeted range has any strings that conform to the pattern of a 1 to 3 digit number followed by a letter from A to M. I need something that will hide any row in which cells(cel.row,20) contains a match for such a pattern.
For example
"SEAT AUDIO JACK LOOSE, WORLD TRAVELLER SEAT 37H.  SEATS AFFECTED  /  IN FLIGHT" would be a positive match and so cel.EntireRow.Hidden = True
"AUDIO ENTIRE CABIN INOP" would not.

I haven't tried using RegEx for 2-3 years so I'm pretty much starting over. Please post the entirety of a sub that would look for pattern matches in Range([T6], [T10000].end(xlUp)).

I think the pattern statement would be something like this but I'm not at all sure:
With RegEx
        .Pattern = "(\d+[A-M])"
        .Global = True
        .IgnoreCase = False

Open in new window



Thanks!

John
LVL 1
John CarneyReliability Business Tools Analyst IIAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
Hi John,

Regex can only accept a string to search, so unfortunately we can only check cell by cell, using something like this....

Sub HideRows()

    Application.ScreenUpdating = False
    
    Dim cell As Range
    Dim Regex
    Set Regex = CreateObject("vbscript.regexp")
    With Regex
        .Pattern = "(\d+[A-M])"
        .Global = True
        .IgnoreCase = False
    End With

    For Each cell In Range([T6], [T10000].End(xlUp))
        cell.EntireRow.Hidden = Regex.Test(cell.Value)
    Next
    
    Application.ScreenUpdating = True
    
End Sub

Open in new window


Wayne
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne Taylor (webtubbs)Commented:
Realised that we needed to modify the regex as well.

Try this regex pattern instead, which matches 1 to 3 digits after a whitespace character.

Sub HideRows()

    Application.ScreenUpdating = False
    
    Dim cell As Range
    Dim Regex
    Set Regex = CreateObject("vbscript.regexp")
    With Regex
        .Pattern = "\s+\d{1,3}[A-M]"
        .Global = True
        .IgnoreCase = False
    End With

    For Each cell In Range([T6], [T10000].End(xlUp))
        cell.EntireRow.Hidden = Regex.Test(cell.Value)
    Next
    
    Application.ScreenUpdating = True
    
End Sub

Open in new window

0
John CarneyReliability Business Tools Analyst IIAuthor Commented:
Perfect! Thanks, Wayne.
0
John CarneyReliability Business Tools Analyst IIAuthor Commented:
Thanks again :- )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.