W
wal
I'm trying to create, in VBA, a function using worksheet functions to
avoid an overly complex formula directly in worksheet cells. But I
keep getting the error "Unable to get the VLookup property of the
WorksheetFunction class"
As a simple example, here is the function if placed in the worksheet
itself (in C10):
=IF(ISNA(VLOOKUP(A10+B10,HolidayList,
1,FALSE)=FALSE),A10+B10,A10+B10+1)
The range "HolidayList" lists dates of holidays. Cell A10 is the
start date; cell B10 is the number of days from the start date; cell
C10 is the deadline I want to find. Basically: If the deadline falls
on a date in "HolidayList" add an extra day.
I converted the formula to VBA, as follows:
***
Function DeadlineDate(StartDate, NumberOfDays%)
Application.Volatile
If WorksheetFunction.IsNA(WorksheetFunction.VLookup(CLng(StartDate) +
_ NumberOfDays, Range("HolidayList"), 1, False)) = True Then
DeadlineDate = CLng(StartDate) + NumberOfDays
Else: DeadlineDate = CLng(StartDate) + NumberOfDays + 1
End If
End Function
***
In C10, the formula is now simply: =deadlinedate(A10,B10)
The problem: Whatever values are in A10 and B10, the Watch I created
for WorksheetFunction.VLookup(StartDate + NumberOfDays,
Range("HolidayList"), 2, False) shows the error value "Unable to get
the VLookup property of the WorksheetFunction class".
Interestingly, if A10 and B10 add up to a date that is found in
HolidayList, the correct deadline date appears in C10, even though the
"Unable to get . . ." error appears in the Watch.
However, if A10 and B10 add up to a date not found on the list, the
value in C10 is #VALUE! (and the "Unable to get . . ." error appears
in the Watch).
Any ideas how to correct the code to make VLookup work in all cases?
Thanks.
avoid an overly complex formula directly in worksheet cells. But I
keep getting the error "Unable to get the VLookup property of the
WorksheetFunction class"
As a simple example, here is the function if placed in the worksheet
itself (in C10):
=IF(ISNA(VLOOKUP(A10+B10,HolidayList,
1,FALSE)=FALSE),A10+B10,A10+B10+1)
The range "HolidayList" lists dates of holidays. Cell A10 is the
start date; cell B10 is the number of days from the start date; cell
C10 is the deadline I want to find. Basically: If the deadline falls
on a date in "HolidayList" add an extra day.
I converted the formula to VBA, as follows:
***
Function DeadlineDate(StartDate, NumberOfDays%)
Application.Volatile
If WorksheetFunction.IsNA(WorksheetFunction.VLookup(CLng(StartDate) +
_ NumberOfDays, Range("HolidayList"), 1, False)) = True Then
DeadlineDate = CLng(StartDate) + NumberOfDays
Else: DeadlineDate = CLng(StartDate) + NumberOfDays + 1
End If
End Function
***
In C10, the formula is now simply: =deadlinedate(A10,B10)
The problem: Whatever values are in A10 and B10, the Watch I created
for WorksheetFunction.VLookup(StartDate + NumberOfDays,
Range("HolidayList"), 2, False) shows the error value "Unable to get
the VLookup property of the WorksheetFunction class".
Interestingly, if A10 and B10 add up to a date that is found in
HolidayList, the correct deadline date appears in C10, even though the
"Unable to get . . ." error appears in the Watch.
However, if A10 and B10 add up to a date not found on the list, the
value in C10 is #VALUE! (and the "Unable to get . . ." error appears
in the Watch).
Any ideas how to correct the code to make VLookup work in all cases?
Thanks.