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
84 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 48

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 31

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 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 31

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

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

13 Experts available now in Live!

Get 1:1 Help Now