VB for Searching for ~ 20 words in a large table of text

Joined
Nov 23, 2023
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi

I am wondering if anybody knows how to replace a grid of formulas with a VBA script ?

I am very familiar using formulas along with VB to obtain results in my spreadsheets. In my current project I

am using a grid of formulas in 20 columns x 20,000 rows to locate and extract data in the data sheet.

It is effectively a complex Search function, with 20 search cells, for up to 20 words at a time.
A typical use case is: you can place a novel with up to 20,000 lines of text in the data sheet. You can enter up

to 20 search words: each word has its own column to Search for that word in each Row.

There is a 21st column that has a formula that equals 1 if the other 20 columns on that row contain a result .

My VB script acts on the 21st column only, looping through the 20k rows, and where it finds a 1, copies the whole row to a Results sheet.

The problem is, while the VB scripts in my project are ~30 KB, the table of 20 x 20,000 formulas add ~15MB to the file size.

I had considered incorporating the formula into the VB script using Application.WorksheetFunction , however that might seriously slow down the script. The formula is long, 2x screen widths of the standard formula Bar and took weeks to construct. Currenltly, Excel natively updates the formula table in about ~10 seconds on first opening, and the VB loops on the 20,000 cells in Col 21 in ~3 seconds.

(The Data table has Range ("A11:A20010") : Search words are in a Range ("A1:T1")

Does anyone know if there's a way, possibly adding the 20 search words to an Array, to quickly check for matches in 20,000 lines, each line containing up to ~100 words, like comparing 2 arrays ? I'm not very familiar with manipulating Arrays. i read somewhere about Intersection or Union of 2 arrays but unsure if its applicable.

My current formulation was slow to set up, however it is methodical and I could progress stepwise over many evenings in a logical fashion. I feel my approach is clunky and old fashioned and I am missing out on a more agile way of getting results and shrinking my file size.

Could someone demonstrate a small example for say a 20 x 20 data table ?

Thanks for any advice on this problem

Rob :)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I can't promise you a solution but Forum members may find it easier to help if you could use the XL2BB add-in (icon in the menu) to attach the desired selected range (not a picture) of your data. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
@BobTheSpreadsheetBuilder Rob, as @mumps has posted, for any member to offer you, even in principle, a worthwhile alternative to what you already have they will need a far better understanding of what you have now and how you use it. Maybe if your methodical, albeit arguably 'clunky' formula / vba approach does the job without any significant issues, you should stick with it?

That said, I do perhaps have a suggestion that might be of value in respect of significantly reducing your file size. Hopefully the below will illustrate the principle?
My example assumes:
Search words to be in B1:U1.
Text to be searched to be A11: A20010 as a MAXIMUM range
*** Your formulas to be Permanent in B11:V11

The vba below will copy formulas from row 11 down only as far as data exists in A and then converts all formulas bar row 11 into values.
Your current vba can then work with those values?
File size will be significantly smaller by saving hard values rather than big formulas.

How you work with your workbook and how will determine if and when you could function with hard values rather than dynamic formulas.
Maybe you add something like it to your current vba?
Maybe you adapt it to call it independently, as and when you need to switch formulas and values?
Maybe have it install the formulas on opening and you accept the file size while working?
Then it could convert the formulas to values on closing the file in order to reduce file size?

Who knows?
TestTime.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1Search Words >>>randomWord2catWord4FigWord6ThisWord8Word9MATWord11flyingWord13Word14Word15Word16Word17Word18Word19Word20
2
3
4
5
6
7
8
9Result to be
10used in vba??
11This is Just a load of random rubish1     1             1
12More random rubish11
13The cat sat on the mat111
14The dog was not pleased , as it was his mat11
15The cat didn't give a flying fig1111
16Surely I have something better to do on aSunday evening?
17More rubish
18More rubish
19More rubish
20More rubish
21More rubish
22More rubish
23More rubish
24More rubish
25More rubish
26More rubish
27More random rubish11
Sheet6
Cell Formulas
RangeFormula
B11:U11B11=IF(IF(OR(B$1="",$A11=""),"",ISNUMBER(FIND(UPPER(B$1),UPPER($A11),1))),1,"")
V11V11= IF(SUM(B11:U11)>0,1,"")


VBA Code:
Sub ReduceFileSize()
Dim lrw As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

lrw = Range("A" & Rows.Count).End(xlUp).Row 'last data row determined b content in column A
If lrw < 12 Then Exit Sub  ' guard against no data in A11:
Range("B12:V20010").ClearContents  'clear to max of 20Krows of data
Range("B11:V" & lrw).Formula = Range("B11:V11").Formula  'extend row 1 formulas to last row
Range("B12:V" & lrw).Value = Range("B12:V" & lrw).Value  'change formulas to hard values in ALL BAR ROW 11 **

'Do some stuff ???
'?????
For Each cell In Range("V11:V" & lrw)
    '?????
        If cell.Value = 1 Then Count = Count + 1
Next cell
     '?????
     
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

HTH
 
Upvote 0
Solution
Hi Snakehips,

thank you for replying - This is exactly what I was looking for, so thank you for spending your Sunday afternoon working on it :)

I haven't seen .Formula before so it certainly makes things a lot simpler ( I thought of using autofill but your .Formula method looks neater) and changing the formulas to fixed values will make the file a lot more compact.

I will spend a few days moving it into my project - lots of formulas so I will double check when I integrate it..

"The cat sat on the mat" ha ha yes i have used sample data like that .. " The dog was not pleased as it was his mat" LoL ... well that's certainly very imaginative !!!

Thank you and enjoy the rest of your Sunday

Rob :cool:
 
Upvote 0

Forum statistics

Threads
1,224,735
Messages
6,180,636
Members
452,992
Latest member
TokugawaIesuma

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