Thanks, but I am looking for range, whereas per Weekday syntax only provide info for specific date
Dim aDayCount(1 to 7) as Integer
To be fair, you can set the array to range from 1-7 - arrays merely default to a lower bound of zero; it's not an actual requirement.
is perfectly valid.Code:Dim aDayCount(1 to 7) as Integer
Private Sub butcal_Click()
Dim DateStart As Date
Dim DateEnd As Date
Dim DateCheck As Date
Dim aDayCount(1 To 7) As Long
Dim DayOfWeek As Long
[COLOR=seagreen]'Pull values from the form.[/COLOR]
DateStart = Me.txtfromdate
DateEnd = Me.txttodate
DateCheck = DateStart
[COLOR=seagreen]'Loop through each date.[/COLOR]
Do Until DateCheck > DateEnd
[COLOR=seagreen]'Determine the day of the week upon which the current date falls.[/COLOR]
DayOfWeek = Weekday(DateCheck)
[COLOR=seagreen]'Increment the indicated element of the array aDayCount by 1.[/COLOR]
aDayCount(DayOfWeek) = aDayCount(DayOfWeek) + 1
[COLOR=seagreen]'Advance to the next date in the date range being checked.[/COLOR]
DateCheck = DateCheck + 1
Loop
[COLOR=seagreen]'All days have been counted, so now insert the data in the form.[/COLOR]
For DayOfWeek = 1 To 7
[COLOR=seagreen]'Determine which weekday it is.[/COLOR]
Select Case DayOfWeek
Case 1 [COLOR=seagreen]'Sunday[/COLOR]
[COLOR=seagreen]'Do whatever you need for each day here. Example follows.[/COLOR]
[COLOR=seagreen] 'Update Me.txtSundayCount.[/COLOR]
Me.txtSundayCount.Value = aDayCount(1)
Case 2 [COLOR=seagreen]'Monday[/COLOR]
Me.txtMondayCount.Value = aDayCount(2)
Case 3 [COLOR=seagreen]'Tuesday[/COLOR]
'Etc, etc etc
Case 4 [COLOR=seagreen]'Wednesday[/COLOR]
Case 5 [COLOR=seagreen]'Thursday[/COLOR]
Case 6 [COLOR=seagreen]'Friday[/COLOR]
Case 7 [COLOR=seagreen]'Saturday[/COLOR]
End Select
Next DayOfWeek
[COLOR=seagreen] 'Update the value of Me.txttotaldays.[/COLOR]
Me.txttotaldays.Value = DateDiff("d", DateStart, DateEnd) + 1
End Sub
Function countDays(StartDate As Date, endDate As Date, thisWeekday As Integer) As Integer
dim StartMonday as date, EndSunday as date
startMonday = StartDate + 8 - Weekday(StartDate, vbMonday)
endSunday = endSunday - Weekday(endDate, vbMonday)
countDays = Int((endDate - startMonday) / 7)
If Weekday(StartDate, vbMonday) <= thisWeekday Then countDays = countDays + 1
If Weekday(endDate, vbMonday) >= thisWeekday Then countDays = countDays + 1
End Function
Sub testit()
Dim i As Integer
For i = 1 To 7
Debug.Print i, countDays(#3/1/2014#, #3/31/2014#, i)
Next i
End Sub
1 5
2 4
3 4
4 4
5 4
6 5
7 5
Don't forget that the total number of days is
Datediff(......)+1
Brian