Getting previous 10 workdays (excluding weekends) from Today (vba)

xs4amit

New Member
Joined
May 21, 2018
Messages
34
I am working on a sheet which requires previous 10 working days from today:

Something like this:

[TABLE="width: 500"]
<tbody>[TR]
[TD]10-Dec[/TD]
[TD]11-Dec[/TD]
[TD]12-Dec[/TD]
[TD]13-Dec[/TD]
[TD]14-Dec[/TD]
[TD]17-Dec[/TD]
[TD]18-Dec[/TD]
[TD]19-Dec[/TD]
[TD]20-Dec[/TD]
[TD]21-Dec[/TD]
[/TR]
</tbody>[/TABLE]

i am scratching my head from last 3 days with all kind of wiered loops but nothing is working. Can anyone help me with a nice VBA code.
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Thanks Guys,

I have cracked it finally, here it goes:


Code:
With WS1




lstwday = Format(Date, "dddd")
Select Case lstwday
Case Is = "Sunday"


.Cells(2, 27) = Date - 2
Case Is = "Saturday"
.Cells(2, 27) = Date - 1
Case Else
.Cells(2, 27) = Date
End Select


Arr1 = .Range("R4:AA4")
Arr2 = .Range("Z2:AA2")
u = 0
For r = 10 To 1 Step -1


strday = Format(Arr2(1, 2) - u, "dddd")
If strday = "Sunday" Then
GoTo ibiza






ibiza:
u = u + 2
Arr1(1, r) = Arr2(1, 2) - u
u = u + 1
Else
Arr1(1, r) = Arr2(1, 2) - u
u = u + 1


End If
Next


.Range("R4:AA4") = Arr1


Let me know if someone can refine it. Improvement ideas are most welcome.
 
Upvote 0
How about:

Code:
Sub testdate()
Dim Arr1(1 To 1, 1 To 10), i As Long

    For i = -9 To 0
        Arr1(1, i + 10) = WorksheetFunction.WorkDay(Date, i)
    Next i
    ActiveSheet.Range("R4:AA4") = Arr1
End Sub
The worksheet function WORKDAY does exactly what you want, and you can call it from VBA.
 
Last edited:
Upvote 0
You can get it even shorter if you want with:

Code:
Sub testdate2()

    Range("R4:AA4").Formula = "=WORKDAY(TODAY(),1-COLUMNS(R4:$AA4))"
    Range("R4:AA4").Value = Range("R4:AA4").Value
    
End Sub
Depending on your ultimate goal, you can just use worksheet formulas too.
 
Upvote 0
Fantastic Eric,

This is phenomenal how you reduced my 10 lines to 3 lines. Thanks


How about:

Code:
Sub testdate()
Dim Arr1(1 To 1, 1 To 10), i As Long

    For i = -9 To 0
        Arr1(1, i + 10) = WorksheetFunction.WorkDay(Date, i)
    Next i
    ActiveSheet.Range("R4:AA4") = Arr1
End Sub
The worksheet function WORKDAY does exactly what you want, and you can call it from VBA.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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