The issue is the Function
parameter types.
When you call your Function with mo
= cell AN1
, containing the number 1
, the Function type casts it to the String "1"
, which doen't exisit in the range Budget!$G$1:$X$1
, since these are also numbers.
The solution is to use Variant
as the function paramters type.
To make debugging this type of error easier, try not to do too much in a single line of code. Splitting the line into 2 x Match
functions and an Index
, would allow you to see the second match return an error.
Couple of other points:
- If your are calling this function as a UDF (ie called from a worksheet cell), it is better not to hard code the ranges. As written, the calls to
eBudgetl
would not automatically recalculate when any of their data changes. - the
Application
object has a version ofIndex
andMatch
so theWorksheetFunction
calls are not required
Refactored version to demonstrate:
Function eBudgetl(rData As Range, rBudCode As Range, rMo As Range, budgetcode As Variant, mo As Variant)
Dim rw As Variant
Dim col As Variant
With Application
col = .Match(budgetcode, rBudCode, 0)
rw = .Match(mo, rMo, 0)
eBudgetl = .Index(rData, col, rw)
End With
End Function
Called as
=eBudgetl(Budget!$G$1:$X$5000,Budget!$B$1:$B$5000,Budget!$G$1:$X$1,$F12,AN$1)