Vlookup in VBA?

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
1,395
Office Version
  1. 2019
  2. 2007
Platform
  1. Windows
Hi all,
I am trying to created a UDF for a complicated (to me) calculation.

In the function I have
Code:
Function CalcFee(TxAmount As Currency) As Currency


Dim iFeeType As Integer


'FeeType indicates whether a set value £200 or a %
iFeeType = WorksheetFunction.VLookup(TxAmount, Sheets("Lookup").Range("A2:E6"), 5, True)
If iFeeType = 3 Then
    IFAFee = Sheets("Lookup").Range("D7").Value
Else
    IFAFee = Round(TxAmount * Sheets("Lookup").Range("D7").Value, 2)
End If


End Function

TxAmount is passed to the function. let's make it 22000 for this example

if I try ? WorksheetFunction.VLookup(TxAmount, Sheets("Lookup").Range("A2:E6"), 5, True) after setting TxAmount in the debug window, I get the correct result.

However when I try ? CalcFee(22000) I get

Run-time error 1004
Unable to get the Vlookup property of the worksheet function class

This is only the start of the code, but I am relying on picking up this value for further calculations?

What am I missing please?

TIA
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi welshgasman,

just change "as currency" to "as double"

Code:
Function CalcFee(TxAmount As Double) As Currency

Dim iFeeType As Integer

'FeeType indicates whether a set value £200 or a %
iFeeType = WorksheetFunction.VLookup(TxAmount, Sheets("Lookup").Range("A2:E6"), 5, 0)
If iFeeType = 3 Then
    CalcFee = Sheets("Lookup").Range("D7").Value
Else
    CalcFee = Round(TxAmount * Sheets("Lookup").Range("D7").Value, 2)
End If

End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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