VLookup issue in macro to calculate workdays and holidays

Z

Zoner

Hi all...

I am looking for a bit of help with the following two functions. I
believe I have a problem with the format of 'date' variables, but am
unsure how to fix it.

I have two functions.
1. WORKDAY - given a start date and a number of days, figures out an
end date, using only weekdays. This function also calls the isHoliday
function, to determine if the day is a holiday, and adds the
corresponding number if days if so.
2. isHoliday - Performs a VLOOKUP into a table list of holidays. The
3rd column in this table lists the number of workdays lost with this
holiday. This function returns 0 if not a holiday, else the number of
days due to this holiday ( normally 1).

My problem is the checkDate variable returns error "Error 2042" on the
Vlookup function on dates that should result in a hit. Othertimes it
may work. Is there a problem between DATE formats and others that may
exist?

Code attached - help appreciated!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function WORKDAY2(start_date As Date, numDays As Integer)
For i = 1 To numDays
start_date = start_date + 1
If Weekday(start_date) = vbSaturday Then
start_date = start_date + 2
End If
' Check for holiday
start_date = start_date + isHoliday(start_date)
' Fix day to monday if holiday was on a Friday
If Weekday(start_date) = vbSaturday Then
start_date = start_date + 2
start_date = start_date + isHoliday(start_date) ' Monday may be
a holiday too!
ElseIf Weekday(start_date) = vbSunday Then
start_date = start_date + 1
start_date = start_date + isHoliday(start_date) ' Monday may be
a holiday too!
End If
Next
WORKDAY2 = start_date
End Function
Function isHoliday(inDate As Date)
Dim checkDate As Variant

checkDate = Application.VLookup(inDate, ThisWorkbook.Worksheets("Time
Summary").Range("hday_list"), 3, False)
If IsError(checkDate) Then
isHoliday = 0
Else
isHoliday = checkDate
Debug.Print "YES - A Holiday: " & inDate & " Returning: " &
checkDate
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top