Search string for sub strings

saltire1963

Board Regular
Joined
Aug 11, 2014
Messages
69
I'm not sure how to fix this.

I've copied my last years worth of credit card statements (2400 rows) into Excel so that I can analyse the data. In each cell in Column D there is text (the narration provided by the Bank) which could be say PAYPAL *NETFLIX.COM 402***** AU and what I would like to do is to search this text string for certain sub strings, say NETFLIX and if a match is found, I would like to categorise it in the cell before in Column C with say the text string "Entertainment". Then move onto all other Rows and categorise each one etc.

If the next row say has any of the multiple words "taxi" or "cab" or "Uber" then I may want to put in Column C the text "Taxi".

SubString = ("taxi", "cab", "Uber")

For i = 3 To finalrow
MainString = Range("D" & i)
If InStr(MainString, SubString) <> 0 Then
Range("C" & i).Value = "Taxi"
End If
Next i

Thanks in advance
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
So, one thing we would need is some sort of list of substrings to search for and their corresponding category. You have given one example of taxi/cab/uber with category taxi but could we have say a couple more?
I would just like to get a realistic feel for how many substrings might be in a category and how many categories there might be.

What should happen if a description happened to contain substrings from more than one category. (eg "Netflix movie Taxi Driver")

Can you confirm that you want this done via macro?
 
Upvote 0
Hi saltire1963,

You could create a rule set like this:

Code:
Option Compare Text 'Makes all text comparisons case insensitive
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long
    Dim lngMyRow As Long
    
    lngLastRow = Cells(Rows.Count, "D").End(xlUp).Row
    
    Application.ScreenUpdating = False
    
    For lngMyRow = 3 To lngLastRow
        'Rule set
        If InStr(Range("D" & lngMyRow), "Netflix") > 0 Then
            Range("C" & lngMyRow).Value = "Entertainment"
        ElseIf InStr(Range("D" & lngMyRow), "Taxi") > 0 Or InStr(Range("D" & lngMyRow), "Cab") > 0 Or InStr(Range("D" & lngMyRow), "Uber") > 0 Then
            Range("C" & lngMyRow).Value = "Taxi"
        End If
    Next lngMyRow
    
    Application.ScreenUpdating = True

End Sub

HTH

Robert
 
Upvote 0
i dont know about VBA but you could try a long and unwieldy formula for column C


Book1
CD
1CategoryNarration
2EntertainmentNetflix
3TaxiUber
4ElectronicsYodobashi
5EntertainmentCineplex
6TaxiYellowcabs
7TakeoutPizza Hut
8OtherBad Dragon
9ElectronicsJBHiFi
10TakeoutCity Wok
11EntertainmentBlockbuster Video
12OtherPaypal
13ElectronicsRadio Shack
14TaxiIndependent Taxi Co
15TakeoutBest Thai
Sheet40
Cell Formulas
RangeFormula
C2=IF(SUMPRODUCT(--ISNUMBER(SEARCH({"Netflix","Cineplex","Video"},D2)))>0,"Entertainment",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"Uber","Cab","Taxi"},D2)))>0,"Taxi",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"Pizza","City Wok","Best Thai"},D2)))>0,"Takeout",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"Yodobashi","Radio Shack","JBHiFi"},D2)))>0,"Electronics","Other"))))
 
Upvote 0
i dont know about VBA but you could try a long and unwieldy formula for column C
I think we need more information about the scale of the problem but a preliminary comment about this formula is that it returns "Taxi" if the OP purchased a "Sword Scabbard" or a bottle of "Cabernet Sauvignon"
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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