Retroshift
Board Regular
- Joined
- Sep 20, 2016
- Messages
- 119
- Office Version
- 2019
- Platform
- Windows
Hi,
I have a VBA code (see below) for a macro button to add month sheets in a workbook.
Now the month sheets are sorted from January to December, but I would like them to be sorted from December to January; while still keeping differently named sheets at the end of the worksheet tabs.
After hitting the macro button, I would like to make an inputbox appear which asks to put a year (starting from the current year, so not earlier than the current year; and only 4 digits are allowed).
And the first row of each month sheet should be populated with all the days (date) of the month in this sheetname, of the year that was entered into the inputbox.
The macro button can only be used once per workbook, unless you manually copy the workbook (for a new year). The code should recognize that the copied workbook is a copy of the original workbook, so the button now can be used again, and the copied month sheets can be overwritten.
Anyone has the VBA knowledge to alter this code accordingly?
I have a VBA code (see below) for a macro button to add month sheets in a workbook.
Now the month sheets are sorted from January to December, but I would like them to be sorted from December to January; while still keeping differently named sheets at the end of the worksheet tabs.
After hitting the macro button, I would like to make an inputbox appear which asks to put a year (starting from the current year, so not earlier than the current year; and only 4 digits are allowed).
And the first row of each month sheet should be populated with all the days (date) of the month in this sheetname, of the year that was entered into the inputbox.
The macro button can only be used once per workbook, unless you manually copy the workbook (for a new year). The code should recognize that the copied workbook is a copy of the original workbook, so the button now can be used again, and the copied month sheets can be overwritten.
Anyone has the VBA knowledge to alter this code accordingly?
VBA Code:
Sub AddMonthSheets()
Dim i As Integer
Dim j As Integer
For i = 1 To 12
If i <= Sheets.Count Then
If Left(Sheets(i).Name, 5) = "Sheet" Then
Sheets(i).Name = MonthName(i)
Else
Sheets.Add.Move after:=Sheets(Sheets.Count)
ActiveSheet.Name = MonthName(i)
End If
Else
Sheets.Add.Move after:=Sheets(Sheets.Count)
ActiveSheet.Name = MonthName(i)
End If
Next i
For i = 1 To 12
If Sheets(i).Name <> MonthName(i) Then
For j = i + 1 To Sheets.Count
If Sheets(j).Name = MonthName(i) Then
Sheets(j).Move Before:=Sheets(i)
End If
Next j
End If
Next i
Sheets(1).Activate
End Sub