Solved

How do I fix my macro to search for a pattern within a string in excel?

Posted on 2015-01-18
4
73 Views
Last Modified: 2015-01-18
I’m trying to create a macro to find the below examples from a string.
Fixed characters
????-AA*-BB*-*-*
ABCD-AAN12-BB3-AG3N5-XYZ123
GKME-AAJK5456-BB36G7-AN5-NJW123YUI

Function MatchSearch (strToSearch)
Set oRegEx = CreateObject("vbscript.regexp")
oRegEx.Global = True
oRegEx.IgnoreCase = True
oRegEx.Pattern = "[A-Z]{4,}-AA*-BB*-*-*"
Set RegExMatches = oRegEx.Execute(strToSearch)
If RegExMatches.Count = 1 Then
    MatchSearch = RegExMatches.Item(0)
Else
    MatchSearch = ""
End If
End Function

Open in new window

0
Comment
Question by:kbay808
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40556809
This should work for you

Function MatchSearch(strToSearch) As String
    Set regex = CreateObject("vbscript.regexp")
    
    With regex
        .Global = True
        .IgnoreCase = True
        .Pattern = "[A-Z]{4}-AA.*-BB.*-.*-.*"
    End With
        
    Set RegExMatches = regex.Execute(strToSearch)
    
    If RegExMatches.Count = 1 Then
        MatchSearch = RegExMatches.Item(0)
    Else
        MatchSearch = ""
    End If
    
End Function

Open in new window


You were just missing the "." to signify any character
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40556966
@kbay808

Do you need to validate the contents beyond the AA and BB sections?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40556972
In addition to the AA and BB, these two patterns match the other sections more closely than the wildcard character.
[A-Z]{4}-AA\w{3,6}-BB\w{1,4}-\w{3,5}-\w{6,9}

Open in new window


or
[A-Z]{4,}-AA\w{3,6}-BB\w{1,4}-[A-Z]{2}\w{1,3}-[A-Z]{3}\w{3,6}

Open in new window

0
 

Author Closing Comment

by:kbay808
ID: 40557030
That worked great!!!  Thank you very much.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now