?
Solved

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

Posted on 2015-01-18
4
Medium Priority
?
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Michael Fowler
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 46

Accepted Solution

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

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

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

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

752 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