Finding Text

RhinoNeil

Board Regular
Joined
Dec 16, 2010
Messages
54
I have a workbook containing downloaded Emails and I now need to identify a particular code appearing somewhere in the Subject field. I am using Excel 2007.

The code will always be 8-numbers and should start and end with a space, unless at start or end of Subject Line (but often people put punctuation instead - e.g. , or ;). There also may be a code of 10-numbers that I do not want to incorrectly identify.

I am using "Like" to identify the pattern but it does not seem to be working correctly.

Code:
 If Subject_Array(Data_Row) Like "*[!#]########[!#]*" = True Or _
            Subject_Array(Data_Row) Like "*[!#]########" = True Or _
            Subject_Array(Data_Row) Like "########[!#]*" = True Then

As I read the help section, the "*[!#]########[!#]*" should look for eight numbers where the characters either side are not a number. The second and third tests are for the cases when the code starts/ends the Subject line.

For the example "Shipment Notification WARE - GSK SOUTH AFRICA - 10097777 - 577758340010001 - AIR" it correctly identifies the "10097777" as valid.
However for "RE: Packing list Notification>Barnard Castle>Israel>FPA: 578707300010001, 578688000000000" it finds the first two tests as TRUE. It results in a code of "00010001"

Can anybody tell me why "Like" gives a TRUE result for the second example and also how to fix it?

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Here's a UDF that may work for you:

Code:
Function find8(sIn) As String
    Dim shold, i
    find8 = ""
    shold = Split(sIn, " ")
    For i = 0 To UBound(shold)
        If shold(i) Like "########" Then
            find8 = shold(i)
            Exit Function
        End If
    Next
End Function

Code:
Sub test()
    MsgBox find8("Shipment Notification WARE - GSK SOUTH AFRICA - 10097777 - 577758340010001 - AIR")
    MsgBox find8("RE: Packing list Notification>Barnard Castle>Israel>FPA: 578707300010001, 578688000000000")
End Sub
 
Upvote 0
tlowry, thanks but if the code was entered with a comma, i.e. "10097777," won't split fail to catch it.
As I said, I need to find an 8 number code which, may or may not start/end with a , or a ; and it cannot take a 10 number code.
Examples of how people enter the code -
" 10097777 "
" 10097777,"
"10097777 - Shipment"
"Shipment - 10097777"
"Shipments 10097777/10097778, Shipment"

All of which I want the code to find, but also ignore -
" 1009777712 "
 
Upvote 0
Without trying to write the code for you (mostly because I don't know what your code is doing necessarily at this point), have you tried to include a like ######## condition AND not like ########## where the second is the longer string of numbers that you don't want?
 
Upvote 0
Next:

Code:
Function find8(sIn) As String
    Dim shold, i
    shold = sIn
    For i = 1 To Len(shold)
        If Not Mid(shold, i, 1) Like "#" Then Mid(shold, i, 1) = " "
    Next i
    Dim arr
    Do While InStr(shold, "  ") > 0
        shold = Replace(shold, "  ", " ")
    Loop
    arr = Split(shold, " ")
    For i = 0 To UBound(arr)
        If arr(i) Like "########" Then
            find8 = arr(i)
            Exit Function
        End If
    Next i
End Function

Sheet1

*AB
1Examples of how people enter the code -*
21009777710097777
3 10097777,10097777
410097777 - Shipment10097777
5Shipment - 1009777710097777
6Shipments 10097777/10097778, Shipment10097777
7**
8**
91009777712*
10**

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:270px;"><col style="width:111px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
B1=find8(A1)
B2=find8(A2)
B3=find8(A3)
B4=find8(A4)
B5=find8(A5)
B6=find8(A6)
B7=find8(A7)
B8=find8(A8)
B9=find8(A9)

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
 
Upvote 0
Thanks for your reply, this will help me get what I want.
I never knew you could "not like" as well as "like" (still very early on with macros). The help function never mentions it so that will help a lot.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,668
Members
449,463
Latest member
Jojomen56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top