Count Number of weekends excluding 2nd and 4th Saturday

GirishDhruva

Active Member
Joined
Mar 26, 2019
Messages
308
Hi Everyone,
Here i am trying to count number of weekends that are available in a particular month excluding 2nd and 4th Saturday

I have the below code to check all the Saturdays and Sundays that are available between two dates but i need to exclude 2nd and 4th Saturday between those two given dates

Code:
 Public Function CountWeekendDays(Date1 As Date, Date2 As Date) As Long    Dim StartDate As Date, EndDate As Date, _
        WeekendDays As Long, i As Long
    If Date1 > Date2 Then
        StartDate = Date2
        EndDate = Date1
    Else
        StartDate = Date1
        EndDate = Date2
    End If
    WeekendDays = 0
    For i = 0 To DateDiff("d", StartDate, EndDate)
        Select Case Weekday(DateAdd("d", i, StartDate))
            Case 1, 7
                WeekendDays = WeekendDays + 1
        End Select
    Next i
    CountWeekendDays = WeekendDays
End Function

dates = CountWeekendDays("8/7/2019", "31/7/2019")
MsgBox ("Number of weekends are" & dates)

Regards
Dhruva
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
See if the following works for you
Code:
Public Function CountWeekendDays(Date1 As Date, Date2 As Date) As Long
    Dim StartDate As Date, EndDate As Date, WeekendDays As Long, i As [COLOR=#ff0000]Date[/COLOR]
    If Date1 > Date2 Then
        StartDate = Date2
        EndDate = Date1
    Else
        StartDate = Date1
        EndDate = Date2
    End If
    WeekendDays = 0
    For i = [COLOR=#ff0000]StartDate To EndDate[/COLOR]
        Select Case Weekday([COLOR=#ff0000]i[/COLOR])
            Case [COLOR=#ff0000]1[/COLOR]
                WeekendDays = WeekendDays + 1
            [COLOR=#ff0000]Case 7
                Select Case Day(i)
                    Case 8 To 14, 22 To 28
                    Case Else
                        WeekendDays = WeekendDays + 1
                End Select[/COLOR]
        End Select
    Next i
    CountWeekendDays = WeekendDays
End Function
 
Upvote 0
Hi @Tetra201 thanks for your effort and it worked perfectly but if the weekly off is 2nd and 4th Saturdays means what changes should i need to make in the code
 
Last edited:
Upvote 0
You are welcome.

I am not sure I correctly understand your request, but if you want to count 2nd and 4th Saturdays and exclude 1st, 3rd, and 5th Saturdays, then
Code:
            ...
            Case 7
                Select Case Day(i)
                    Case 8 To 14, 22 To 28
                        WeekendDays = WeekendDays + 1
                End Select
                ...
 
Upvote 0
I am facing an compile time error in highlighted line, could anyone suggest me what changes i should make
Rich (BB code):
Option Explicit


Public Function CountWeekendDays(Date1 As Date, Date2 As Date) As Long
    Dim StartDate As Date, EndDate As Date, WeekendDays As Long, i As Date
    If Date1 > Date2 Then
        StartDate = Date2
        EndDate = Date1
    Else
        StartDate = Date1
        EndDate = Date2
    End If
    WeekendDays = 0
    For i = StartDate To EndDate
        Select Case Weekday(i)
            Case 1
                WeekendDays = WeekendDays + 1
            Case 7
                Select Case Day(i)
                    Case 8 To 14, 22 To 28
                        WeekendDays = WeekendDays + 1
                    Case Else
                End Select
        End Select
    Next i
    CountWeekendDays = WeekendDays
End Function


Sub try()
Dim Dates, Lastrow As Long
Dim Date1, Date2 As Date
Dim Main_Sheet As Worksheet


Set Main_Sheet = Worksheets("Sheet1")
With Main_Sheet
    Date1 = InputBox("Type in the Start Date you want to add", "Start Date Of Month", "Start Date Of Month")
    Date2 = InputBox("Type in the End Date you want to add", "End Date Of Month", "End Date Of Month")
    Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Dates = CountWeekendDays(Date1, Date2)
    .Range("P3:P" & Lastrow).Value = Dates
End With
End Sub

Regards,
Dhruva
 
Upvote 0
Try
Code:
Sub try()
Dim Dates [COLOR=#ff0000]As Long[/COLOR], Lastrow As Long
Dim Date1 [COLOR=#ff0000]As Date[/COLOR], Date2 As Date
...
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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