VBA Evaluate & VLOOKUP

mse330

Well-known Member
Joined
Oct 18, 2007
Messages
785
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I have been using Evaluate() in my VBA code recently to understand how it works & enjoy the potential of this function. However, I just ran into a case where I was unable to make it work correctly using VLOOKUP. I have made a small sample & used MATCH function to ensure my syntax is correct & the MATCH function works as expected ?

After further inspection, I noticed that the VLOOKUP uses table_array whereas MATCH uses lookup_array (as you can see in the screen shot) which I am not sure if this is what is causing my code to give incorrect results

Does anyone have any explanation for this or how to make my evaluate with VLOOKUP to work correctly ?

VBA Code:
Sub test()
With Range("C2:C4")
   .Offset(, 1) = Evaluate("if({1},vlookup(" & .Address & ",$A$2:$A$5,1,0))")
   .Offset(, 4) = Evaluate("if({1},match(" & .Address & ",$A$2:$A$5,0))")
End With
End Sub

Copy of Worksheets.xlsm
ABCDEFGH
1NamesNamesEvaluateVlookUpEvaluateMatch
2JackSaraSaraSara22
3SaraJeffSara#N/A#N/A#N/A
4MikeFredSaraFred44
5Fred
Sheet1
Cell Formulas
RangeFormula
E2:E4E2=VLOOKUP(C2,$A$2:$A$5,1,0)
H2:H4H2=MATCH(C2,$A$2:$A$5,0)



evaluate.png
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try it like
VBA Code:
Sub test()
With Range("C2:C4")
   .Offset(, 1) = Evaluate("if({1},vlookup(t(if({1}," & .Address & ")),$A$2:$A$5,1,0))")
   .Offset(, 4) = Evaluate("if({1},match(" & .Address & ",$A$2:$A$5,0))")
End With
End Sub
 
Upvote 0
@Fluff Thank you very much ... Always very fast response & right to the point. I was searching before posting a new thread & found a 10-page thread from 2014 HERE which I lost track while I was reading

Again, much appreciated & have a nice weekend
 
Upvote 0
Not surprised you lost track. Reading any thread involving the Doc, makes me loose the will to live. ;)
 
Upvote 0
Have you read any of his posts. ;)
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,497
Members
448,967
Latest member
visheshkotha

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