Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA to automatically save the workbook every 2 minutes 3 29
Excel 3 22
Convert .PDF 6 38
Name Rotation 11 24
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 simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

708 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

21 Experts available now in Live!

Get 1:1 Help Now