Solved

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

Posted on 2015-01-18
4
79 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

696 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