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
91 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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