Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
100 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
[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
  • 2
  • 2
6 Comments
 
LVL 52

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 33

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 52

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 33

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

722 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