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
88 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 49

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 32

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

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 32

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 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

12 Experts available now in Live!

Get 1:1 Help Now