worksheetfunction Lookup

christian2016

Board Regular
Joined
Oct 6, 2016
Messages
123
Hi Guys,

Im trying to use the WorksheetFunction Lookup to store the calculation within the VBA code rather than in a cell within the worksheet.
For some reason I cant get the below code working in VBA although in my sheet its working correctly.

In my worksheet the formula is =IFERROR(LOOKUP(2,1/($B$3:$B$21<=E3)/($C$3:$C$21>=E3),$A$3:$A$21),"Invalid Postcode") and this is working well.

Any help is greatly appreciated.

Thanks


VBA Code:
Dim Postcode As String

Postcode = 3000
  x = WorksheetFunction.Lookup(2, 1 / (Range("B3:B21") <= Postcode) / (Range("C3:C21") >= Postcode), Range("A3:A21"), "Invalid Postcode")
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Your worksheet function is wrapped by the IFERROR function. Your vba code shouldn't have the ", "Invalid Postcode")" part because that is a parameter of the IFERROR function which is not being used in the vba statement.

Try it like this with x as variant data type

VBA Code:
x = WorksheetFunction.Lookup(2, 1 / (Range("B3:B21") <= Postcode) / (Range("C3:C21") >= Postcode), Range("A3:A21")
if IsError(x) then x = "Invalid PostCode"
 
Upvote 0
Your worksheet function is wrapped by the IFERROR function. Your vba code shouldn't have the ", "Invalid Postcode")" part because that is a parameter of the IFERROR function which is not being used in the vba statement.

Try it like this with x as variant data type

VBA Code:
x = WorksheetFunction.Lookup(2, 1 / (Range("B3:B21") <= Postcode) / (Range("C3:C21") >= Postcode), Range("A3:A21")
if IsError(x) then x = "Invalid PostCode"
Thank you. I get type mismatch error on the worksheetfunction.lookup
 
Upvote 0
Change WorksheetFunction to Application
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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