Quicker method to run macro for keywords

jackal764u

New Member
Joined
Jul 15, 2019
Messages
11
Hello and thank you for a most helpful excel site.

This macro looks for keywords in a memo column and replaces adjacent columns with applicable content from a keyword sheet.
The memo column contains 14300 rows to search and match to 3500 rows in keyword column.
This is on windows 7 and Excel 2010 Pro
It takes the macro just under 3 minutes to complete. Is it possible to make it quicker?

KW + Memo.png


VBA Code:
Sub AddCategories2()
Dim wsIDs As Worksheet, Keywords As Range, Word As Range
Dim kFIND As Range, kFIRST As Range

Application.ScreenUpdating = False
'Choose Active Sheet Below
Set wsIDs = Sheets("Keywords")
'Where to Find the Keywords
Set Keywords = wsIDs.Range("F2:Z" & Rows.Count).SpecialCells(xlConstants)

On Error Resume Next
'Choose Active Sheet
With Sheets("Orig Memo + New cat")
    If MsgBox("Reset IDs before running new search?", vbYesNo, "Reset") = vbYes Then .Range("C2:F" & .Rows.Count).ClearContents
    For Each Word In Keywords
'    Find Keywords in Item Description/Memo Column
'        Set kFIND = .Range("B:B").Find(Word.Text, LookIn:=xlValues, LookAt:=xlPart)
        Set kFIND = .Range("B:B").Find(Word.Text, LookIn:=xlValues)
        If Not kFIND Is Nothing Then
            Set kFIRST = kFIND
            Do
                If .Range("C" & kFIND.Row) = "" Then
'                    fill col D(Payee) & E(Cat) & F(SubCat) in Orig Memo + New cat sheet with col D(Payee) & E(Cat) & F(SubCat) from Keywords sheet.
'                    where to paste cols(with C=1)  . 2nd resize is cols to copy from Keywords sheet
                    .Range("C" & kFIND.Row).Resize(, 3).Value = wsIDs.Range("C" & Word.Row).Resize(, 3).Value
                    .Range("F" & kFIND.Row).Resize(, 1).Value = wsIDs.Range("F" & Word.Row).Resize(, 1).Value
                End If
                        
'                Go to Next Row (Loop)
                Set kFIND = .Range("B:B").FindNext(kFIND)
            Loop Until kFIND.Address = kFIRST.Address
            Set kFIRST = Nothing
        End If
    Next Word
End With

Application.ScreenUpdating = True
End Sub


Thank you. Your help is appreciated
 
Yes, the code should take those spaces into consideration, although it is not case-sensitive (not sure if that's a problem).

Can you post some sample data from both sheets that demonstrates the problem.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
Tx. Here goes the XL2BB attempt

First is the "Orig Memo + New cat" sheet

Report Experiments V11 Test from MrExcel.xlsm
CDEF
1327Vehicle RepaymentsVehicle RepaymentsPolo TDiPolo Tdi Settlement
1328MaintenanceMaintenanceGlass
1329HealthcareHealthcarePharmacyPha
1330PetsPetsPet
1331FuelFuelService Centr
1332FuelFuelService Centr
1333FuelFuelService Centr
1334FuelFuelService Centr
1335FuelFuelService Centr
1336FuelFuelService Centr
1337FuelFuelService Centr
1338Sanbonani ExpensesSanbonani ExpensesCar RentalRent A Car
1339FuelFuelService Centr
1340Len SalaryLen SalaryHydro DoorsCams Payment FNB
1341Len SalaryLen SalaryHydro DoorsCams Payment FNB
1342Len SalaryLen SalaryHydro DoorsCams Payment FNB
1343Len SalaryLen SalaryHydro DoorsCams Payment FNB
1344Len SalaryLen SalaryHydro DoorsCams Payment FNB
1345Len SalaryLen SalaryHydro DoorsCams Payment FNB
1346Len SalaryLen SalaryHydro DoorsCams Payment FNB
1347Len SalaryLen SalaryHydro DoorsCams Payment FNB
1348FuelFuelMotor
1349FuelFuelService Centr
1350InterestInterestLen Cheque FNBINT
1351InterestInterestLen Cheque FNBINT
1352FuelFuelService Centr
1353FuelFuelMotor
1354HealthcareHealthcarePharmacyESSENTIAL HEALTH
1355HealthcareHealthcarePharmacyESSENTIAL HEALTH
1356HealthcareHealthcarePharmacyESSENTIAL HEALTH
Orig Memo + New cat



This is the "Keywords" sheet

Report Experiments V11 Test from MrExcel.xlsm
CDEF
2650Vehicle RepaymentsVehicle RepaymentsPolo TDiPolo Tdi Settlement
2651GroceriesGroceriesPolokwane ZA
2652GroceriesGroceriesPOPPIES
2653RestaurantRestaurantPORTERHOUSE
2654MaintenanceMaintenanceC"pennypinchers
2655GroceriesGroceries
2656Sanbonani ExpensesSanbonani ExpensesStationeryPost Office
2657General ExpenseGeneral ExpenseStationeryPostnet
2658Take AwayTake AwayPotbelly
2659Leisure And EntertainmentLeisure and entertainmentLottoPOWERBALL
2660Vehicle MaintenanceVehicle MaintenanceExhaustPOWERFLOW
2661Tech GoodsTech GoodsPoynting
2662GroceriesGroceriesPp*4856code
2663Inter Account Transfer FromInter Account Transfer FromLen Cheque FNBPremier Cheque
2664Bank FeesBank FeesCard Feeprepaid airtime fee
2665GroceriesGroceriesPresident Hyper
2666HealthcareHealthcarePharmacyPriarnnacy
2667Sanbonani ExpensesSanbonani ExpensesAirline TicketsPrimacare
2668FuelFuelPrime Stop
2669Take AwayTake AwayPrimi PiattI
2670Sanbonani ExpensesSanbonani ExpensesFuelPrincess Palace
2671Bank FeesProperty Service ProviderAdvertisingPrivate Property
2672ComputerComputerComputer HardwarePro Graphics
2673Leisure And EntertainmentLeisure and entertainmentSports EquipmentPro Shop
2674Sanbonani ExpensesSanbonani ExpensesTender DocPROJECTS
2675Property Service ProviderProperty Service ProviderTrust FeesProkereur
2676FuelFuelProper Serv
2677RentRentSilverfieldsProprent
2678HealthcareHealthcarePharmacyPROTEA HOOGTE BRACKENFELL ZA
2679HealthcareHealthcarePsychiatristPsych
Keywords
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Thanks for that, can you re-post the data for the Orig Memo + New cat sheet, including col B?
 
Upvote 0
Thanks for that, can you re-post the data for the Orig Memo + New cat sheet, including col B?
Sorry, my mistake

Report Experiments V11 Test from MrExcel.xlsm
BCDEF
13271bb Polo Tdi Settlement Vods65dx8 9YBVehicle RepaymentsVehicle RepaymentsPolo TDiPolo Tdi Settlement
132875221004258000000970042 C*Bellville Glass CentMaintenanceMaintenanceGlass
132975412834086000180038062 Klein Panorama Pharm President Swart StrHealthcareHealthcarePharmacyPha
1330Absolute Pets Century Century City ZAPetsPetsPet
1331Basson’s Service Centr Witpoorijie ZAFuelFuelService Centr
1332Basson’s Service Centr Witpoortjie ZAFuelFuelService Centr
1333Basson�s Service Centr Witpoortjie ZAFuelFuelService Centr
1334Basson's Service Centr Witooortiie ZAFuelFuelService Centr
1335Basson's Service Centr Witpoorijie ZAFuelFuelService Centr
1336Basson's Service Centr Witpoortjie ZAFuelFuelService Centr
1337Basson's Service Centr Witpoortjie ZA ,FuelFuelService Centr
1338Budget Rent A Car Isando ZASanbonani ExpensesSanbonani ExpensesCar RentalRent A Car
1339C*basson's Service Centr Witpoortjie ZAFuelFuelService Centr
1340Cams Payment FNB Cams Pay 00568 SalaryLen SalaryLen SalaryHydro DoorsCams Payment FNB
1341Cams Payment FNB Cams Pay 00575 SalaryLen SalaryLen SalaryHydro DoorsCams Payment FNB
1342Cams Payment FNB Cams Pay 00583 SalaryLen SalaryLen SalaryHydro DoorsCams Payment FNB
1343Cams Payment FNB Cams Pay 00591 SalaryLen SalaryLen SalaryHydro DoorsCams Payment FNB
1344Cams Payment FNB Cams Pay 00599 SalaryLen SalaryLen SalaryHydro DoorsCams Payment FNB
1345Cams Payment FNB Cams Pay 00607 SalaryLen SalaryLen SalaryHydro DoorsCams Payment FNB
1346Cams Payment FNB Cams Pay 00615 SalaryLen SalaryLen SalaryHydro DoorsCams Payment FNB
1347Cams Payment FNB Cams Pay 00623 SalaryLen SalaryLen SalaryHydro DoorsCams Payment FNB
1348Circle Centre Motors F Monavoni ZAFuelFuelMotor
1349Clive's Service Centre Isando ZAFuelFuelService Centr
1350Electronlc Payments Bis/Int 1 On True Tiering = 6.30InterestInterestLen Cheque FNBINT
1351Electronlc Payments Bis/Int 4 On True llerlng = 33.20InterestInterestLen Cheque FNBINT
1352Empire Service Centre Bassonia ZAFuelFuelService Centr
1353Enterpirse Motors Johannesburg ZAFuelFuelMotor
1354Essential Health Phy Kac Kuils RiverHealthcareHealthcarePharmacyESSENTIAL HEALTH
1355ESSENTIAL HEALTH PHY KACKUILS RIVERHealthcareHealthcarePharmacyESSENTIAL HEALTH
1356ESSENTIAL HEALTH PR 400974*8637 05 JUNHealthcareHealthcarePharmacyESSENTIAL HEALTH
Orig Memo + New cat
 
Upvote 0
Thanks for that, the problem is that F2655 is blank which is causing the problem, this will ignore any rows where col F is blank
VBA Code:
Sub jackal()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, i As Long
   
   With Sheets("Keywords")
      Ary = .Range("C2", .Range("F" & Rows.Count).End(xlUp)).Value2
   End With
   With Sheets("Orig Memo + New cat")
      If MsgBox("Reset IDs before running new search?", vbYesNo, "Reset") = vbYes Then .Range("C2:F" & .Rows.Count).ClearContents
      Nary = .Range("B2:F" & .Range("B" & Rows.Count).End(xlUp).Row)
   End With
   For r = 1 To UBound(Nary)
      For i = 1 To UBound(Ary)
         If Ary(i, 4) <> "" And InStr(1, Nary(r, 1), Ary(i, 4), vbTextCompare) Then
            Nary(r, 2) = Ary(i, 1)
            Nary(r, 3) = Ary(i, 2)
            Nary(r, 4) = Ary(i, 3)
            Nary(r, 5) = Ary(i, 4)
            Exit For
         End If
      Next i
   Next r
   Sheets("Orig Memo + New cat").Range("B2").Resize(UBound(Nary), 5).Value = Nary
End Sub
 
Upvote 0
Solution
Thanks for that, the problem is that F2655 is blank which is causing the problem, this will ignore any rows where col F is blank
VBA Code:
Sub jackal()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, i As Long
  
   With Sheets("Keywords")
      Ary = .Range("C2", .Range("F" & Rows.Count).End(xlUp)).Value2
   End With
   With Sheets("Orig Memo + New cat")
      If MsgBox("Reset IDs before running new search?", vbYesNo, "Reset") = vbYes Then .Range("C2:F" & .Rows.Count).ClearContents
      Nary = .Range("B2:F" & .Range("B" & Rows.Count).End(xlUp).Row)
   End With
   For r = 1 To UBound(Nary)
      For i = 1 To UBound(Ary)
         If Ary(i, 4) <> "" And InStr(1, Nary(r, 1), Ary(i, 4), vbTextCompare) Then
            Nary(r, 2) = Ary(i, 1)
            Nary(r, 3) = Ary(i, 2)
            Nary(r, 4) = Ary(i, 3)
            Nary(r, 5) = Ary(i, 4)
            Exit For
         End If
      Next i
   Next r
   Sheets("Orig Memo + New cat").Range("B2").Resize(UBound(Nary), 5).Value = Nary
End Sub
I was not aware that it could cause errors (F2655). Sorry.

Thank you so much for your assistance and expertise!
Your solution is exactly what was required. It is accurate, correct and effective.
You have cut the macro time from 3 minutes to below 30 seconds.

I commend you on your expertise and willingness to help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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