List of all working days between two dates

StefaniaPana

New Member
Joined
Apr 1, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I found the below VBA that returns a list of all the days between working days that works perfectly.
But I need it to return only the working days, is there a line or smth that I need to add to do so?

Sub WriteDates()

Dim rng As Range
Dim StartRng As Range
Dim EndRng As Range
Dim OutRng As Range
Dim StartValue As Variant
Dim EndValue As Variant
xTitleId = "KutoolsforExcel"
Set StartRng = Application.Selection
Set StartRng = Application.InputBox("Start Range (single cell):", xTitleId, StartRng.Address, Type:=8)
Set EndRng = Application.InputBox("End Range (single cell):", xTitleId, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set OutRng = OutRng.Range("A1")
StartValue = StartRng.Range("A1").Value
EndValue = EndRng.Range("A1").Value
If EndValue - StartValue <= 0 Then
Exit Sub
End If
ColIndex = 0
For i = StartValue To EndValue
OutRng.Offset(ColIndex, 0) = i
ColIndex = ColIndex + 1
Next
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this code. I also declared your undeclared variables (use Option Explicit). (Also please note how helpful code tags are.) Note that this does not take holidays into account. If you have a list of holidays then you can modify the If statement accordingly.

Rich (BB code):
Sub WriteDates()

   Dim rng As Range
   Dim StartRng As Range
   Dim EndRng As Range
   Dim OutRng As Range
   Dim StartValue As Variant
   Dim EndValue As Variant
   Dim xTitleId As String
   Dim ColIndex As Long
   Dim i As Long
  
   xTitleId = "KutoolsforExcel"
   Set StartRng = Application.Selection
   Set StartRng = Application.InputBox("Start Range (single cell):", xTitleId, StartRng.Address, Type:=8)
   Set EndRng = Application.InputBox("End Range (single cell):", xTitleId, Type:=8)
   Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
   Set OutRng = OutRng.Range("A1")
   StartValue = StartRng.Range("A1").Value
   EndValue = EndRng.Range("A1").Value
  
   If EndValue - StartValue <= 0 Then
      Exit Sub
   End If
  
   ColIndex = 0
  
   For i = StartValue To EndValue
      If Weekday(i, vbSaturday) > 2 Then
         OutRng.Offset(ColIndex, 0) = i
         ColIndex = ColIndex + 1
      End If
   Next
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,344
Members
448,570
Latest member
rik81h

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