From MSDN:
The advantage of using square brackets is that the code is shorter. The advantage of using
Evaluate
is that the argument is a string, so you can either construct the string in your code or use a Visual Basic variable.
in other words you can use
Calculate = [VLOOKUP(3, A1:B100, 2)]
but you can not use
LookupValue = 3
LookupRange = Range("A1:B100")
'or
'LookupRange = "A1:B100"
Calculate = [VLOOKUP(LookupValue, LookupRange, 2)]
What you can do is:
Option 1:
Function Calculate(LookupValue As Double, LookupRange As Range) As Double
Calculate = Evaluate("VLOOKUP(" & LookupValue & "," & LookupRange.Address & ", 2")
End Function
or better:
Function Calculate(LookupValue As Double, LookupRange As Range) As Double
Calculate = Evaluate("VLOOKUP(" & LookupValue & ",'" & _
LookupRange.Parent.Name & "'!" & LookupRange.Address & ", 2")
End Function
However I suggest:
Option 2:
Function Calculate(LookupValue As Double, LookupRange As Range) As Double
Calculate = Application.VLookup(LookupValue, LookupRange, 2)
End Function
I hope you know about meaning of 4th parameter:
If
TRUE
or omitted, an exact or approximate match is returned. If an exact match is not found, the next largest value that is less than lookup_value is returned. The values in the first column of table_array must be placed in ascending sort order; otherwise,VLOOKUP
may not give the correct value. You can put the values in ascending order by choosing the Sort command from the Data menu and selecting Ascending.
Btw, Calculate
is not good name for UDF, since VBA already has function Application.Calculate
. I'd rename it to avoid confusion.