Dynamic Search - Number of rows

Exact

New Member
Joined
Mar 11, 2025
Messages
7
Office Version
  1. 365
Platform
  1. MacOS
  2. Web
Hi, I have a table with 2 columns (L & M), the number of rows in the table can change so I want to search using the current number of rows.

For a fixed number of rows (7) this formula works
Range("I1").Formula2 = "=IFERROR(LOOKUP(2,1/SEARCH($L$2:$L$7,$D1),$M$2:$M$7),$E1)"

But I can't figure out how to make $L$7 and $M$7 dynamic

I use this to get the current number of rows "x":
x = Cells(Rows.Count, "L").End(xlUp).Row

I've tried a number of ways but can't get the right syntax. e.g.
Range("I1").Formula2 = "=IFERROR(LOOKUP(2,1/SEARCH(range("$L$2:$L$" &x,$D1),range("$M$2:$M$" &x),$E1)"
Range("I1").Formula2 = "=IFERROR(LOOKUP(2,1/SEARCH(range("$L$2"),"$L$" & x,$D1),range("$M$2","$M$" &x),$E1)"
Range("I1").Formula2 = "=IFERROR(LOOKUP(2,1/SEARCH((range("L2","L" & x)).address,$D1),(range("M2","M" &x)).address,$E1)"
Range("I1").Formula2 = "=IFERROR(LOOKUP(2,1/SEARCH("("&range("L2","L" & x&")").address,$D1),"("&range("M2","M" &x&)")".address,$E1)"

I'd appreciate any help.
 
Please try the following:

Excel Formula:
Range("I1").Formula2 = "=IFERROR(LOOKUP(2,1/SEARCH($L$2:$L$" & x & ",$D1),$M$2:$M$" & x & "),$E1)"
 
Upvote 0
Solution
I find it much easier and make less mistakes if i don't try to string together the formula parts and variable(s) but make the formula much more like the actual worksheet formula with symbols in place of the missing/unknown values and then replace those symbols with the relevant values.
So, for me it would be more like this where the red part is just like the final worksheet formula but with # in place of the unknown row number, then replace # with the value held in the variable x

Rich (BB code):
Range("I1").Formula2 = Replace("=IFERROR(LOOKUP(2,1/SEARCH($L$2:$L$#,$D1),$M$2:$M$#),$E1)", "#", x)
 
Upvote 0

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