Performance question - string search across many cells

bbrother

New Member
Joined
Nov 23, 2015
Messages
41
I have about 10 cells in a row that contain strings. These strings can be null to paragraphs of text. I need to find out if a particular key word exists in any of the cells, for each row through hundreds of thousands of rows, then this gets nested in another loop - so I need this to be fast! My first step is to read all of the rows into an array. (Doing any operations direct in cells is a performance killer!) The question is- Should I concatenate all of the elements of the array for the row and run instr once to look for the string, or should I loop through each element of the row and perform the instr test on each one? I suspect that the latter method is faster.
Code:
dim snap() as variantdim i as integer  'my rowdim k as integer  'my columndim HitList as new collectionset snap = range("A1:Z99999")   'for example - some populated rangefor i = 1 to ubound(snap)     if instr(snap(i,1) & snap(i,2) & snap(i,3) & snap(i,4) & snap(i,5), "SearchWord") > 0 then                HitList.add snap(i,1) snap(i,1) 'create a collection with no duplicates     end ifnext'ORfor i = 1 to ubound(snap)     for k = 1 to ubound(snap,2)           if instr(snap(i,k) > 0 then                     hitlist.add snap(i,1) snap(i,1)  'create a collection with no duplicates                    exit for  'short circuit exit - any hit is good enough                      end ifnext 'now I can loop through my hitlist
Which of the two techniques for creating hitlist above is faster?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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