Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to create a vba code that will search a string in cell “A1” for a value that starts with “ABC” and is 11 charters long?

Posted on 2014-12-10
6
Medium Priority
?
101 Views
Last Modified: 2014-12-10
Here is are some examples:

Example 1
Ticket Number:ABC12345678 Site:NMKDS

Example 2
Tkt ABC43634564 Code ND87

Example 3
***ABC34634645***
0
Comment
Question by:kbay808
  • 2
  • 2
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 40490729
Hi,

pls try

Sub RegEx_Tester()
Set oRegEx = CreateObject("vbscript.regexp")
oRegEx.Global = True
oRegEx.IgnoreCase = True
oRegEx.Pattern = "ABC[0-9]{8,}"
strToSearch = "ddABC12345678ddd" ' Range("A1").Value
Set RegExMatches = oRegEx.Execute(strToSearch)
If RegExMatches.Count = 1 Then
MsgBox ("This substring has a value of: ") & RegExMatches.Item(0)
End If
End Sub

Open in new window

Regards
0
 
LVL 34

Expert Comment

by:Rob Henson
ID: 40490847
You can use the SEARCH/FIND function in a formula:

=FIND("ABC????????",A1,1)

The ? represents a single character, unlike * which represents any character or string of characters.

Thanks
Rob H
0
 

Author Comment

by:kbay808
ID: 40491907
I can't get either of your solutions to work.  I attached example file with the 3 examples available via a dropdown menu so that it will be easier to test.

Rgonzo1971: When I changed the code to "strToSearch = Range("A1").Value" there was no result.  Also, I need the result to be entered into cell "C2".

Rob: For all 3 examples, the formula failed to work.  Also, if no result is found could you make it where the result is not an error?
Search-Exampe.xlsm
0
Industry Leaders: 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!

 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 40491922
Changed the code to be a function

Function RegEx_Tester(strToSearch)
Set oRegEx = CreateObject("vbscript.regexp")
oRegEx.Global = True
oRegEx.IgnoreCase = True
oRegEx.Pattern = "ABC[0-9]{8,}"
Set RegExMatches = oRegEx.Execute(strToSearch)
If RegExMatches.Count = 1 Then
    RegEx_Tester = RegExMatches.Item(0)
Else
    RegEx_Tester = ""
End If
End Function

Open in new window


see example


Regards
Search-ExampeV1.xlsm
0
 
LVL 34

Expert Comment

by:Rob Henson
ID: 40491958
Wrap the formula within an IFERROR function.
0
 

Author Closing Comment

by:kbay808
ID: 40492013
It works great.  Thanks for your help.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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.

916 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