Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default vba coding question

I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how do
I construct the loop to check first if its a weekend day and if so run again
to see if the day prior is a weekend day and then if that is also a weekend
day to check if day prior again is a holiday. If it is, return the next prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default vba coding question

What was wrong with the code I sent in your last posting? The requirments
are very similar to this requuest. did your requirements change or was there
something wrong with the rpevious code.

"thomas donino" wrote:

I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how do
I construct the loop to check first if its a weekend day and if so run again
to see if the day prior is a weekend day and then if that is also a weekend
day to check if day prior again is a holiday. If it is, return the next prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default vba coding question

I couldnt get it to work and instead built 5 custom functions to perform
various tasks. This task is to identify if the target date is a business day.
I am doing this by using the IsWeekend function first. If true, then I need
to check the date prior, if it is also a weekend then I check the date prior
to that to see if its a holiday,if it wasn't it returns that day. If the day
prior was a holiday then I return the day prior to that day.

Here are the functions code;

Public Function IsHoliday(dttoCheck As Date) As Boolean

Dim myval As Variant
myval = Application.Match(CLng(dttoCheck), Range("HolidayDates"), 0)

If IsError(myval) Then
IsHoliday = False
Else
IsHoliday = True
End If

End Function


Public Function IsWeekend(dttoCheck As Date) As Boolean
If Weekday(dttoCheck) = 1 Or Weekday(dttoCheck) = 7 Then
IsWeekend = True
Else
IsWeekend = False
End If
End Function

I did it this way because I felt I could use these functions in other projects





"Joel" wrote:

What was wrong with the code I sent in your last posting? The requirments
are very similar to this requuest. did your requirements change or was there
something wrong with the rpevious code.

"thomas donino" wrote:

I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how do
I construct the loop to check first if its a weekend day and if so run again
to see if the day prior is a weekend day and then if that is also a weekend
day to check if day prior again is a holiday. If it is, return the next prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default vba coding question

On Mon, 10 Aug 2009 14:47:02 -0700, thomas donino
wrote:

I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how do
I construct the loop to check first if its a weekend day and if so run again
to see if the day prior is a weekend day and then if that is also a weekend
day to check if day prior again is a holiday. If it is, return the next prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance


Excel has this function either built-in (Excel 2007+) or as part of the
analysis tool pack.

For a worksheet function, with your date to process in A1, and Holidays being a
named range of holiday dates, merely use:

=WORKDAY(A1+1,-1,Holidays)

--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default vba coding question

thank you

"Ron Rosenfeld" wrote:

On Mon, 10 Aug 2009 14:47:02 -0700, thomas donino
wrote:

I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how do
I construct the loop to check first if its a weekend day and if so run again
to see if the day prior is a weekend day and then if that is also a weekend
day to check if day prior again is a holiday. If it is, return the next prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance


Excel has this function either built-in (Excel 2007+) or as part of the
analysis tool pack.

For a worksheet function, with your date to process in A1, and Holidays being a
named range of holiday dates, merely use:

=WORKDAY(A1+1,-1,Holidays)

--ron



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default vba coding question

I think this will do what you want...

Function GetDate(D As Date) As Date
GetDate = D + (Weekday(D, vbMonday) 5) + (Weekday(D, vbMonday) 6)
GetDate = GetDate + IsHoliday(GetDate)
End Function

Note that I do not make use of your IsWeekend function in this code... I
only use your IsHoliday function. Also note those plus signs are all correct
(VB True values evaluate to -1, so adding them subtracts days).

--
Rick (MVP - Excel)


"thomas donino" wrote in message
...
I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how
do
I construct the loop to check first if its a weekend day and if so run
again
to see if the day prior is a weekend day and then if that is also a
weekend
day to check if day prior again is a holiday. If it is, return the next
prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default vba coding question

Does there happen to be a last day of the month function too?

"Ron Rosenfeld" wrote:

On Mon, 10 Aug 2009 14:47:02 -0700, thomas donino
wrote:

I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how do
I construct the loop to check first if its a weekend day and if so run again
to see if the day prior is a weekend day and then if that is also a weekend
day to check if day prior again is a holiday. If it is, return the next prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance


Excel has this function either built-in (Excel 2007+) or as part of the
analysis tool pack.

For a worksheet function, with your date to process in A1, and Holidays being a
named range of holiday dates, merely use:

=WORKDAY(A1+1,-1,Holidays)

--ron

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default vba coding question

I see you used pieces of my previous code. And I like that you put the
holidays on the worksheet in a named range. I used an Array because it was
easier to do it in array when posting the results. You still will need a
very complicated IF statement in the workbook to get the results you need.

I combined your code and my code together to get 3 functions. Make sure the
cell that is returned s formated as a date otherwise you will get a number.
the code is tested and should work.. I also tested the last code. that is
why I'm thinking you either are not passing a real date into the function, or
you simply have to format the cell as a date. You were probably getting a
number results.


Public Function IsHoliday(dttoCheck As Date) As Boolean

Dim myval As Variant
Set myval = Range("HolidayDates").Find(what:=dttoCheck, _
LookIn:=xlValues, lookat:=xlWhole)

If myval Is Nothing Then
IsHoliday = False
Else
IsHoliday = True
End If

End Function


Public Function IsWeekend(dttoCheck As Date) As Boolean
If Weekday(dttoCheck) = 1 Or Weekday(dttoCheck) = 7 Then
IsWeekend = True
Else
IsWeekend = False
End If
End Function

Function Prior3Days(Target As Date) As Date

' only process data theat is a date
If IsDate(Target) Then

Prior3Days = Target
CountDays = 3
Do While CountDays 0
'subtract 1 day
Prior3Days = Prior3Days - 1
'check if the day is a weekend
If Not IsWeekend(Prior3Days) Then

'check if the day is a holiday

If Not IsHoliday(Prior3Days) Then
CountDays = CountDays - 1
End If
End If
Loop
End If
End Function




"thomas donino" wrote:

I couldnt get it to work and instead built 5 custom functions to perform
various tasks. This task is to identify if the target date is a business day.
I am doing this by using the IsWeekend function first. If true, then I need
to check the date prior, if it is also a weekend then I check the date prior
to that to see if its a holiday,if it wasn't it returns that day. If the day
prior was a holiday then I return the day prior to that day.

Here are the functions code;

Public Function IsHoliday(dttoCheck As Date) As Boolean

Dim myval As Variant
myval = Application.Match(CLng(dttoCheck), Range("HolidayDates"), 0)

If IsError(myval) Then
IsHoliday = False
Else
IsHoliday = True
End If

End Function


Public Function IsWeekend(dttoCheck As Date) As Boolean
If Weekday(dttoCheck) = 1 Or Weekday(dttoCheck) = 7 Then
IsWeekend = True
Else
IsWeekend = False
End If
End Function

I did it this way because I felt I could use these functions in other projects





"Joel" wrote:

What was wrong with the code I sent in your last posting? The requirments
are very similar to this requuest. did your requirements change or was there
something wrong with the rpevious code.

"thomas donino" wrote:

I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how do
I construct the loop to check first if its a weekend day and if so run again
to see if the day prior is a weekend day and then if that is also a weekend
day to check if day prior again is a holiday. If it is, return the next prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default vba coding question

This returns whatever date is in the cell. I am looking to take the month
from the date and find the last day of that month

"Rick Rothstein" wrote:

I think this will do what you want...

Function GetDate(D As Date) As Date
GetDate = D + (Weekday(D, vbMonday) 5) + (Weekday(D, vbMonday) 6)
GetDate = GetDate + IsHoliday(GetDate)
End Function

Note that I do not make use of your IsWeekend function in this code... I
only use your IsHoliday function. Also note those plus signs are all correct
(VB True values evaluate to -1, so adding them subtracts days).

--
Rick (MVP - Excel)


"thomas donino" wrote in message
...
I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how
do
I construct the loop to check first if its a weekend day and if so run
again
to see if the day prior is a weekend day and then if that is also a
weekend
day to check if day prior again is a holiday. If it is, return the next
prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default vba coding question

Rick, my prior reply was to my question as to whether or not there is a
function that returns the last day of a given month. Your code was for my
first post I guess. I was already instructed to use the =workday function,
which is working great.

"Rick Rothstein" wrote:

I think this will do what you want...

Function GetDate(D As Date) As Date
GetDate = D + (Weekday(D, vbMonday) 5) + (Weekday(D, vbMonday) 6)
GetDate = GetDate + IsHoliday(GetDate)
End Function

Note that I do not make use of your IsWeekend function in this code... I
only use your IsHoliday function. Also note those plus signs are all correct
(VB True values evaluate to -1, so adding them subtracts days).

--
Rick (MVP - Excel)


"thomas donino" wrote in message
...
I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how
do
I construct the loop to check first if its a weekend day and if so run
again
to see if the day prior is a weekend day and then if that is also a
weekend
day to check if day prior again is a holiday. If it is, return the next
prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance





  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default vba coding question

On Mon, 10 Aug 2009 16:20:01 -0700, thomas donino
wrote:

Does there happen to be a last day of the month function too?

"Ron Rosenfeld" wrote:

On Mon, 10 Aug 2009 14:47:02 -0700, thomas donino
wrote:

I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how do
I construct the loop to check first if its a weekend day and if so run again
to see if the day prior is a weekend day and then if that is also a weekend
day to check if day prior again is a holiday. If it is, return the next prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance


Excel has this function either built-in (Excel 2007+) or as part of the
analysis tool pack.

For a worksheet function, with your date to process in A1, and Holidays being a
named range of holiday dates, merely use:

=WORKDAY(A1+1,-1,Holidays)

--ron


=eomonth(some_date,0) will give the last day of the month of some_date

If either of these give the NAME error, check HELP for the function for how to
correct it.
--ron
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default vba coding question

Rick How do you handle the case when the data is a monday and a holiday is on
Friday. Or the date is Saturday and and the prior Thursday is Thanksgiving?

"Rick Rothstein" wrote:

I think this will do what you want...

Function GetDate(D As Date) As Date
GetDate = D + (Weekday(D, vbMonday) 5) + (Weekday(D, vbMonday) 6)
GetDate = GetDate + IsHoliday(GetDate)
End Function

Note that I do not make use of your IsWeekend function in this code... I
only use your IsHoliday function. Also note those plus signs are all correct
(VB True values evaluate to -1, so adding them subtracts days).

--
Rick (MVP - Excel)


"thomas donino" wrote in message
...
I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how
do
I construct the loop to check first if its a weekend day and if so run
again
to see if the day prior is a weekend day and then if that is also a
weekend
day to check if day prior again is a holiday. If it is, return the next
prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance



  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default vba coding question

Here are two functions, Isholiday, which works with IsBizDay.
IsBizDay returns true if its not a weekend day or a holiday. You will need a
list of holidays in a column for this to work

Public Function IsHoliday(dttoCheck As Date) As Boolean

Dim myval As Variant
myval = Application.Match(CLng(dttoCheck), Range("HolidayDates"), 0)

If IsError(myval) Then
IsHoliday = False
Else
IsHoliday = True
End If

End Function
Public Function IsBizDay(Targdate As Date) As Boolean

Dim dt As Variant

'dt = DateValue(Weekday(Targdate))
dt = Weekday(Targdate)
If dt 1 And dt < 7 And (IsHoliday(Targdate) = False) Then
IsBizDay = True
Else
IsBizDay = False
End If

End Function


Then you can use If IsBizDay then ..........



"Joel" wrote:

Rick How do you handle the case when the data is a monday and a holiday is on
Friday. Or the date is Saturday and and the prior Thursday is Thanksgiving?

"Rick Rothstein" wrote:

I think this will do what you want...

Function GetDate(D As Date) As Date
GetDate = D + (Weekday(D, vbMonday) 5) + (Weekday(D, vbMonday) 6)
GetDate = GetDate + IsHoliday(GetDate)
End Function

Note that I do not make use of your IsWeekend function in this code... I
only use your IsHoliday function. Also note those plus signs are all correct
(VB True values evaluate to -1, so adding them subtracts days).

--
Rick (MVP - Excel)


"thomas donino" wrote in message
...
I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is how
do
I construct the loop to check first if its a weekend day and if so run
again
to see if the day prior is a weekend day and then if that is also a
weekend
day to check if day prior again is a holiday. If it is, return the next
prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance



  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default vba coding question

Good question! I think this (untested) code should handle all the
possibilities...

Function GetDate(D As Date) As Date
GetDate = D
Do
GetDate = GetDate + (Weekday(GetDate, vbMonday) 5) + _
(Weekday(GetDate, vbMonday) 6)
GetDate = GetDate + IsHoliday(GetDate)
Loop While Weekday(GetDate, vbMonday) 5 Or IsHoliday(GetDate)
End Function

--
Rick (MVP - Excel)


"Joel" wrote in message
...
Rick How do you handle the case when the data is a monday and a holiday is
on
Friday. Or the date is Saturday and and the prior Thursday is
Thanksgiving?

"Rick Rothstein" wrote:

I think this will do what you want...

Function GetDate(D As Date) As Date
GetDate = D + (Weekday(D, vbMonday) 5) + (Weekday(D, vbMonday) 6)
GetDate = GetDate + IsHoliday(GetDate)
End Function

Note that I do not make use of your IsWeekend function in this code... I
only use your IsHoliday function. Also note those plus signs are all
correct
(VB True values evaluate to -1, so adding them subtracts days).

--
Rick (MVP - Excel)


"thomas donino" wrote in message
...
I have a date in a cell which I need to check for whether or not it is a
weekend day or a holiday. I have a function to do each, my question is
how
do
I construct the loop to check first if its a weekend day and if so run
again
to see if the day prior is a weekend day and then if that is also a
weekend
day to check if day prior again is a holiday. If it is, return the next
prior
day and if not then return that day. I already built the Isweekend and
Isholiday functions.

Thank you in advance




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro/VB coding question melExcel2007 Excel Discussion (Misc queries) 3 June 4th 10 11:16 PM
Coding Question Jcraig713 Excel Programming 1 April 2nd 09 09:58 PM
VBA Coding Question Matt Excel Programming 3 January 30th 08 12:03 PM
Coding question bach Excel Programming 4 September 19th 05 08:59 PM
coding question No Name Excel Programming 1 September 28th 04 05:26 PM


All times are GMT +1. The time now is 04:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"