Display Workdays ONLY

BillTony

Board Regular
Joined
May 3, 2017
Messages
52
Hi there,

The code below displays / outputs a list all dates between a specified starting and ending date input by the user.

However, I'm having a bit of trouble only listing WORKDAYS (Monday through Friday).

An added BONUS would be to also eliminate Holidays from the list.

Any advice or assistance would be GREATLY appreciated!

Code:
Sub Date_Generator()
'Create some VARIABLES.
    Dim Start_Date As Date
    Dim End_Date As Date
    Dim Next_Date As Date
'Set the Column Width.
    Columns("A:C").ColumnWidth = 20
'Assign some VALUES.
    Start_Date = Range("B1").Value
    End_Date = Range("B2").Value
    Next_Date = Start_Date
    
'Set the OUTPUT Destination.
    Range("C1").Select
    
'Set the PARAMETERS of the DATE RANGE.
    Do Until Next_Date >= End_Date
    
    ActiveCell.Value = Next_Date
    ActiveCell.Offset(1, 0).Select
    Next_Date = Next_Date + 1
    
'Tell it to INCREMENT
    Loop
    
    
End Sub
 
Last edited:

FormR

MrExcel MVP
Joined
Aug 18, 2011
Messages
6,216
Office Version
365
Platform
Windows
Hi, here is some code you can try:

Code:
Sub Date_Generator()
'Create some VARIABLES.
    Dim Start_Date As Date
    Dim End_Date As Date
    Dim Total_Workdays As Long
'Set the Column Width.
    Columns("A:C").ColumnWidth = 20
'Assign some VALUES.
    Start_Date = Range("B1").Value
    End_Date = Range("B2").Value
    Total_Workdays = Application.NetworkDays(Start_Date, End_Date, Range("Holidays"))
'OUTPUT the results.
    With Range("C1").Resize(Total_Workdays)
        .Cells = Evaluate("choose({1},workday(" & CLng(Start_Date) - 1 & ",row(1:" & Total_Workdays & "), holidays))")
    End With
End Sub
You need to have a named range called holidays that refers to a list of holiday dates.
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,661
Office Version
365
Platform
Windows
Try this
Create a range containing your holiday dates and amend Sheets("Sheet5").Range("A1:A8")


Code:
Sub Date_Generator()
'Create some VARIABLES.
    Dim Start_Date As Date
    Dim End_Date As Date
    Dim Next_Date As Date
    Dim w As Long
    Dim Cel As Range
'Set the Column Width.
    Columns("A:C").ColumnWidth = 20
'Assign some VALUES.
    Start_Date = Range("B1").Value
    End_Date = Range("B2").Value
    Next_Date = Start_Date
    
'Set the OUTPUT Destination.
    Range("C1").Select
    
'Set the PARAMETERS of the DATE RANGE.
    Do Until Next_Date >= End_Date
        For Each Cel In [COLOR=#ff0000]Sheets("Sheet5").Range("A1:A8")[/COLOR]
            If Next_Date = Cel Then Next_Date = Next_Date + 1
        Next Cel
        w = Weekday(Next_Date)
        If w > 1 And w < 7 Then
            ActiveCell.Value = Next_Date
            ActiveCell.Offset(1, 0).Select
        End If
        Next_Date = Next_Date + 1
    
'Tell it to INCREMENT
    Loop
    
End Sub
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,274
Try this:
This does not eliminate Holidays.
Code:
Sub Date_Generator()
    'Modified  3/7/2019  8:40:37 AM  EST
Application.ScreenUpdating = False
    Dim Start_Date As Date
    Dim DD As Long
    Dim x As Long
    Dim ans As String
    x = 0
    Dim End_Date As Date
    Dim Next_Date As Date
'Set the Column Width.
Columns("A:C").ColumnWidth = 20
    Start_Date = Range("B1").Value
    End_Date = Range("B2").Value
    Next_Date = Start_Date
    ans = DateDiff("d", Start_Date, End_Date)
    
        For i = 1 To ans + 1
            DD = Weekday(DateAdd("d", i - 1, Start_Date))
                If DD <> 7 And DD <> 1 Then
                x = x + 1
                Cells(x, "C").Value = DateAdd("d", i - 1, Start_Date)
                End If
        Next
    Application.ScreenUpdating = True
End Sub
 

BillTony

Board Regular
Joined
May 3, 2017
Messages
52
Awesome, works like a charm...

Many thanks FormR!
 
Last edited:

Forum statistics

Threads
1,081,973
Messages
5,362,505
Members
400,677
Latest member
champchamp

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top