How to Identify the End of the 4th Week of a Month ?? - 2

monirg

Well-known Member
Joined
Jan 11, 2005
Messages
629
<HR>
Note:
This is a re-post of my earlier Thread by the same Title (posted: Fri, July 08, 2011, ~ 3:30 PM EST.)
My Original Post and ALL replies had vanished from the Forum. I've notified the Admin, and can assure you it was NOT my fault!
I was about to try both the macro procedure and the w/s procedure, when I was advised that the Thread had been killed off by mistake by a rookie moderator on the Forum!
I'm doing my best here to remember the words and phrases I posted earlier, and I'd appreciate re-posting your earlier reply; if you still have it.
Thanks a lot.


<HR>

Hello;

My question may sound trivial to some, but it isn't really.

1) I need to identify the date of Friday of the 4th Week of a given month of a given year.
For example:
.... it is 23 for September, 2011
.... and it is 22 for October 2012.

2) I thought of creating a full 12-month calendar for each year of interest, and manually highlighting the date of Friday of the 4th week of each month.
I immediately realized that it would be messy and susceptible to errors!

3) I suppose one can create a smart macro to do just that, but more neatly:
... accepting a valid name for the month (i.e.; January to December)
... accepting a valid year (say; 2011 to 2021)
... and returning the date, something like:
..... "End of 4th Week: Friday, 28 September, 2012"

4) Alternatively, a w/s procedure using the Date functions, and no macros, may also do the trick:
... select the month & year from drop-down lists (say, starting C10 & D10 Sheet1)
... (month & year data validation ranges compiled on Sheet2)
... and the Date formula returns in cell C5 Sheet1 the date; something like:
..... "End of 4th Week: Friday, 26 October, 2012"

5) What if the above identified Friday is a Bank Holiday ??
... (may use a bogus holiday list for now)
... The w/s Date formula should instead return something like:
..... "End of 4th Week: Thursday, 25 October, 2012 (Friday is a Bank Holiday)"
... Does this add an unnecessary complication at this stage, and I should worry about it later ??

Can someone please provide some guidance on how-to, or provide a link to relevant macro / worksheet / Date formula, if already exist.

Thank you kindly.
 
Hi all,

I have been corresponding privately with Monrig since the first thread performed it's vanishing act. I have devised a solution based on a user form. I will supply it here for the benefit of those following the thread.

On the user form there are four combo boxes:
Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] UserForm_Initialize()
  [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
  
  [COLOR=green]'populate the comboboxes[/COLOR]
  
  [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 7
    [COLOR=darkblue]With[/COLOR] cboDay
      .AddItem i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
  [COLOR=darkblue]Next[/COLOR] i
  
  [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 12
    [COLOR=darkblue]With[/COLOR] cboMonth
    .AddItem i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
  [COLOR=darkblue]Next[/COLOR] i
  
  [COLOR=darkblue]With[/COLOR] cboYear
    [COLOR=darkblue]For[/COLOR] i = 2010 [COLOR=darkblue]To[/COLOR] 2020
      .AddItem i
    [COLOR=darkblue]Next[/COLOR] i
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
  
  [COLOR=darkblue]With[/COLOR] cboNth
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 4
      .AddItem i
    [COLOR=darkblue]Next[/COLOR] i
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
And two command buttons, cmdClose and cmdProcess.

The cmdProcess procedure checks that valid values have been selected from the comboboxes and then calls a function.

NB the function can be used as a stand alone function by placing it inside a standard module.

The function takes the values of the combo box as arguments:
NB The function can test for the 1st, 2nd, 3rd and 4th instance of a day for any month in any year. I haven't coded for a five week month.

Code:
Private Function  nthDayOfMonth(ByVal iDay As Integer,  _
                                 ByVal  iMonth As Integer,  _
                                 ByVal iYear As Integer,  _
                                 ByVal iNth As Integer) As  Date
We now process the data entered starting at the first day of the month:
Code:
'start at the first  day of the selected month
 DayCount =  1
We have a counter for each instance of the day selected
And loop until this counter = nTh occurance:

Code:
Do  Until Count = iNth
We convert the input values into a date.
The DateValue() function works on strings so we need to convert our values to strings using the CStr() function.
Code:
'convert input  parameters to a date value
      dteTemp = DateValue(CStr(DayCount) & "/" & CStr(iMonth) & "/" &  CStr(iYear))
We check the day of the week using the Weekday() function:
Code:
'check for the day  of the week starting from Sunday
      DayOfWeek = Weekday(dteTemp, vbSunday)
This is then checked against the input day and the instance counter incremented:
Code:
 'check against the  input day from the combo box
     If  DayOfWeek = iDay Then
        'increment the  instance counter
        Count = Count + 1
      End If
And then we go to the next day of the month:
Code:
 'go to the next day  of the month
      DayCount = DayCount + 1
This is repeated until the condition for our Do…Loop is met.
Code:
Do  Until Count = iNth
The full code for the user form is shown below:

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Function[/COLOR] nthDayOfMonth([COLOR=darkblue]ByVal[/COLOR] iDay [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR], _
                               [COLOR=darkblue]ByVal[/COLOR] iMonth [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR], _
                               [COLOR=darkblue]ByVal[/COLOR] iYear [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR], _
                               [COLOR=darkblue]ByVal[/COLOR] iNth [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]) [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Date[/COLOR]

  [COLOR=darkblue]Dim[/COLOR] dteTemp [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Date[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] DayCount [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] Count [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] DayOfWeek [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]  [COLOR=green]'Sun=1, Mon=2 etc[/COLOR]
  
  [COLOR=green]'start at the first day of the selected month[/COLOR]
  DayCount = 1
  
  [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] Count = iNth
    
    [COLOR=green]'convert input parameters to a date value[/COLOR]
    dteTemp = DateValue(CStr(DayCount) & "/" & [COLOR=darkblue]CStr[/COLOR](iMonth) & "/" & CStr(iYear))
  
    [COLOR=green]'check for the day of the week starting from Sunday[/COLOR]
    DayOfWeek = Weekday(dteTemp, vbSunday)
    
    [COLOR=green]'check against the input day from the combo box[/COLOR]
    [COLOR=darkblue]If[/COLOR] DayOfWeek = iDay [COLOR=darkblue]Then[/COLOR]
      [COLOR=green]'increment the instance counter[/COLOR]
      Count = Count + 1
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    [COLOR=green]'go to the next day of the month[/COLOR]
    DayCount = DayCount + 1
  [COLOR=darkblue]Loop[/COLOR]
  
  nthDayOfMonth = dteTemp
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Function[/COLOR]

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] cmdProcess_Click()
  [COLOR=darkblue]Dim[/COLOR] dteDate [COLOR=darkblue]As[/COLOR] Date
  [COLOR=darkblue]Dim[/COLOR] iDay [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] iMonth [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] iYear [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] iNth [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
  
  
  [COLOR=green]'==========================================================[/COLOR]
  [COLOR=green]'validate the user has selected somthing from each combobox[/COLOR]
  
  [COLOR=darkblue]If[/COLOR] cboDay.Value = "" [COLOR=darkblue]Then[/COLOR]
    MsgBox "Please select a the Day to process!"
    [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
  [COLOR=darkblue]Else[/COLOR]
    iDay = [COLOR=darkblue]CInt[/COLOR](cboDay.Value)
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
  
  [COLOR=darkblue]If[/COLOR] cboMonth.Value = "" [COLOR=darkblue]Then[/COLOR]
    MsgBox "Please select a the Month to process!"
    [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
  [COLOR=darkblue]Else[/COLOR]
    iMonth = [COLOR=darkblue]CInt[/COLOR](cboMonth.Value)
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
  
  [COLOR=darkblue]If[/COLOR] cboYear.Value = "" [COLOR=darkblue]Then[/COLOR]
    MsgBox "Please select a the Year to process!"
    [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
  [COLOR=darkblue]Else[/COLOR]
    iYear = [COLOR=darkblue]CInt[/COLOR](cboYear.Value)
  [COLOR=darkblue]End[/COLOR] If
  
  If cboNth.Value = "" [COLOR=darkblue]Then[/COLOR]
    MsgBox "Please select a the Nth to process!"
    [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
  [COLOR=darkblue]Else[/COLOR]
    iNth = [COLOR=darkblue]CInt[/COLOR](cboNth.Value)
  [COLOR=darkblue]End[/COLOR] If
  [COLOR=green]'==========================================================[/COLOR]
  
  dteDate = nthDayOfMonth(iDay, iMonth, iYear, iNth)
  
  [COLOR=green]'output[/COLOR]
  MsgBox dteDate
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]



[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] cmdClose_Click()
  Unload Me
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]



[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] UserForm_Initialize()
  [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
  
  [COLOR=green]'populate the comboboxes[/COLOR]
  
  [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 7
    [COLOR=darkblue]With[/COLOR] cboDay
      .AddItem i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
  [COLOR=darkblue]Next[/COLOR] i
  
  [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 12
    [COLOR=darkblue]With[/COLOR] cboMonth
    .AddItem i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
  [COLOR=darkblue]Next[/COLOR] i
  
  [COLOR=darkblue]With[/COLOR] cboYear
    [COLOR=darkblue]For[/COLOR] i = 2010 [COLOR=darkblue]To[/COLOR] 2020
      .AddItem i
    [COLOR=darkblue]Next[/COLOR] i
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
  
  [COLOR=darkblue]With[/COLOR] cboNth
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 4
      .AddItem i
    [COLOR=darkblue]Next[/COLOR] i
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi Bertie;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
It works perfectly! Thanks again for your tremendous help and generous time.

Regards.<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,451
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top