0

I want to get the date of Monday for each week. I am using that date to open a folder that is created every Monday and is named based on the date. I tried using weekday() function. But I am not sure how to proceed. Thanks for the help! This is what i have tried, it works. But I juts wanted to know if there is more efficient way to do it?

Sub test()

Dim myday As Integer
Dim mydate As Date
mydate = Date
myday = Weekday(Date, vbMonday)

If myday = 1 Then

mydate = Date

ElseIf myday = 2 Then
mydate = DateAdd("d", -1, Date)

ElseIf myday = 3 Then
mydate = DateAdd("d", -2, Date)

ElseIf myday = 4 Then
mydate = DateAdd("d", -3, Date)

ElseIf myday = 5 Then
mydate = DateAdd("d", -4, Date)

End If

End Sub
Community
  • 1
  • 1
Ashonna
  • 47
  • 7

2 Answers2

3

If DT is any date, and if you want to return the same date if DT is a Monday, then, using VBA:

Function FirstMonday(DT As Date) As Date
    FirstMonday = DT - Weekday(DT, vbMonday) + 1
End Function

Or, on your worksheet, with the Date in A1:

=A1+1-WEEKDAY(A1-1)
Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
0

In A1 enter:

1/6/2014

In A2 enter:

=A1 + 7

and copy down

Gary's Student
  • 95,722
  • 10
  • 59
  • 99