sakrams
Board Regular
- Joined
- Sep 28, 2009
- Messages
- 59
- Office Version
- 2016
- Platform
- Windows
Good Morning Excel Gurus,
I would really appreciate your support with the below excel VBA script to achieve the following scenario. For eg. Year 2024, I need a full list of week number with the dates as below (concatenated) for easy copy-paste.
Week starts from Monday to Sunday. For e.g.
(Week 1) - 01-Jan-2024 to 07-Jan-2024
(Week 2) - 08-Jan-2024 to 14-Jan-2024
I have been trying the following script, but getting compilation errors (Microsoft Excel 365). Any help from the gurus will be highly appreciated. Thanks in advance.
----------------------------------
Sub GenerateWeekList()
Dim year As Integer
Dim startDate As Date
Dim endDate As Date
Dim weekStartDate As Date
Dim weekEndDate As Date
Dim weekNumber As Integer
Dim i As Integer
' Input the year
year = InputBox("Enter the year:")
' Clear existing data
Range("A2:B" & Rows.Count).ClearContents
' Set starting date of the year
startDate = DateSerial(year, 1, 1)
' Loop through each week of the year
i = 0
Do
weekNumber = Application.WorksheetFunction.ISOWEEKNUM(startDate)
weekStartDate = WorksheetFunction.WorkDay(startDate, 0)
weekEndDate = WorksheetFunction.WorkDay(startDate, 6)
' Output week number and dates to worksheet
Cells(i + 2, 1).Value = "(Week " & weekNumber & ")"
Cells(i + 2, 2).Value = weekStartDate & " to " & weekEndDate
' Move to the next week
startDate = WorksheetFunction.WorkDay(startDate, 7)
i = i + 1
Loop While Year(startDate) = year
End Sub
I would really appreciate your support with the below excel VBA script to achieve the following scenario. For eg. Year 2024, I need a full list of week number with the dates as below (concatenated) for easy copy-paste.
Week starts from Monday to Sunday. For e.g.
(Week 1) - 01-Jan-2024 to 07-Jan-2024
(Week 2) - 08-Jan-2024 to 14-Jan-2024
I have been trying the following script, but getting compilation errors (Microsoft Excel 365). Any help from the gurus will be highly appreciated. Thanks in advance.
----------------------------------
Sub GenerateWeekList()
Dim year As Integer
Dim startDate As Date
Dim endDate As Date
Dim weekStartDate As Date
Dim weekEndDate As Date
Dim weekNumber As Integer
Dim i As Integer
' Input the year
year = InputBox("Enter the year:")
' Clear existing data
Range("A2:B" & Rows.Count).ClearContents
' Set starting date of the year
startDate = DateSerial(year, 1, 1)
' Loop through each week of the year
i = 0
Do
weekNumber = Application.WorksheetFunction.ISOWEEKNUM(startDate)
weekStartDate = WorksheetFunction.WorkDay(startDate, 0)
weekEndDate = WorksheetFunction.WorkDay(startDate, 6)
' Output week number and dates to worksheet
Cells(i + 2, 1).Value = "(Week " & weekNumber & ")"
Cells(i + 2, 2).Value = weekStartDate & " to " & weekEndDate
' Move to the next week
startDate = WorksheetFunction.WorkDay(startDate, 7)
i = i + 1
Loop While Year(startDate) = year
End Sub