Input Dates Within A date range Excluding Weekends

Nathan95

New Member
Joined
Mar 1, 2020
Messages
34
Office Version
  1. 2010
Platform
  1. Windows
Hey All

I have the bellow code which takes the inputted start and end date along with the other inputted fields and and lists them besides each individual date within that range on a table in a different sheet.

The issue is that this will list every single date within the range, However I want it to only list the weekdays and not the weekends (Sat and Sun). Any help would be greatly appreciated.

Private Sub cbInputleave_Click()

Dim FirstDate As Date, LastDate As Date
Dim rBlanks As Range
Dim ssheet As Worksheet
Dim ctl As Control

Set ssheet = ThisWorkbook.Sheets("Mark Leave")

FirstDate = tbstartdate.Value
LastDate = tbenddate.Value

With ssheet.Range("D" & Rows.Count).End(xlUp).Offset(1).Resize(LastDate - FirstDate + 1)
.Value = Evaluate("Row(" & CLng(FirstDate) & ":" & CLng(LastDate) & ")")
.NumberFormat = "dd-mmm-yy"
.Offset(, 1).Value = cbLeaveType.Value
.Offset(, -2).Value = tbPnumber.Value
.Offset(, -3).Value = ssheet.Evaluate(.Offset(, -2).Address & "&" & .Address)
End With

MsgBox "Leave Entered"

Unload Me




End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
There are lots of unknown factors/variable (tbstartdate,tbenddate,cbLeaveType...,), without worksheet sample, its not help a lot.
What are they?
 
Upvote 0
Try to loop from FirstDate to LastDate, using WEEKDAY function to exclude weekend, then read into a variable array, then write back into sheet
Not test, but try:
VBA Code:
Private Sub cbInputleave_Click()
Dim i As Long, k As Long, arr() ' NEW
Dim FirstDate As Date, LastDate As Date
Dim rBlanks As Range
Dim ssheet As Worksheet
Dim ctl As Control
Set ssheet = ThisWorkbook.Sheets("Mark Leave")
FirstDate = tbstartdate.Value
LastDate = tbenddate.Value

'---new added from here
ReDim arr(1 To LastDate - FirstDate + 1, 1 To 1) 
For i = FirstDate To LastDate
    If Weekday(i, 2) < 6 Then
        k = k + 1
        arr(k, 1) = i
    End If
Next
With ssheet.Range("D" & Rows.Count).End(xlUp).Offset(1).Resize(UBound(arr), 1) ' NEW
.Value = arr
'-----------
.NumberFormat = "dd-mmm-yy"
.Offset(, 1).Value = cbLeaveType.Value
.Offset(, -2).Value = tbPnumber.Value
.Offset(, -3).Value = ssheet.Evaluate(.Offset(, -2).Address & "&" & .Address)
End With
MsgBox "Leave Entered"
Unload Me
End Sub
 
Upvote 0
Solution
Try to loop from FirstDate to LastDate, using WEEKDAY function to exclude weekend, then read into a variable array, then write back into sheet
Not test, but try:
VBA Code:
Private Sub cbInputleave_Click()
Dim i As Long, k As Long, arr() ' NEW
Dim FirstDate As Date, LastDate As Date
Dim rBlanks As Range
Dim ssheet As Worksheet
Dim ctl As Control
Set ssheet = ThisWorkbook.Sheets("Mark Leave")
FirstDate = tbstartdate.Value
LastDate = tbenddate.Value

'---new added from here
ReDim arr(1 To LastDate - FirstDate + 1, 1 To 1)
For i = FirstDate To LastDate
    If Weekday(i, 2) < 6 Then
        k = k + 1
        arr(k, 1) = i
    End If
Next
With ssheet.Range("D" & Rows.Count).End(xlUp).Offset(1).Resize(UBound(arr), 1) ' NEW
.Value = arr
'-----------
.NumberFormat = "dd-mmm-yy"
.Offset(, 1).Value = cbLeaveType.Value
.Offset(, -2).Value = tbPnumber.Value
.Offset(, -3).Value = ssheet.Evaluate(.Offset(, -2).Address & "&" & .Address)
End With
MsgBox "Leave Entered"
Unload Me
End Sub
Perfect thanks alot, that worked like a charm :)

I had another query listed bellow, wondering if you are able to assist with this :)

 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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