Retroshift
Board Regular
- Joined
- Sep 20, 2016
- Messages
- 119
- Office Version
- 2019
- Platform
- Windows
Hello,
I have VBA code with an inputbox where you can enter specific years in order to create worksheets containing the months of this year.
Now, if the year is entered twice, then the sub should be exited with the messagebox "This year has already been entered". Anyone has the VBA knowledge to complete the code below accordingly? The line ".Name = MonthName(i, True) & Right(yr, 4)" seems to cause an issue with duplicate years and I would like to prevent this.
I have VBA code with an inputbox where you can enter specific years in order to create worksheets containing the months of this year.
Now, if the year is entered twice, then the sub should be exited with the messagebox "This year has already been entered". Anyone has the VBA knowledge to complete the code below accordingly? The line ".Name = MonthName(i, True) & Right(yr, 4)" seems to cause an issue with duplicate years and I would like to prevent this.
VBA Code:
Sub AddMonthSheets()
Dim yr As String
Dim i As Integer
Dim rng As Range
yr = InputBox("For which year do you want to create month sheets?", "Enter a year", "e.g. " & Year(Date))
If (yr) Like "[2-9][0-9][0-9][0-9]" Then
ElseIf StrPtr(yr) = 0 Then
Exit Sub
Else
MsgBox "Enter a year value between 1999 and 9999"
Exit Sub
End If
For i = 1 To 12 Step 1
Sheets.Add.Move after:=Sheets(Sheets.Count)
With ActiveSheet
[SIZE=4].Name = MonthName(i, True) & Right(yr, 4)[/SIZE]
.Range("B1") = DateSerial(yr, i, 1)
.Range("B1").NumberFormat = "[$-en-GB]ddd"
.Range("B1").AutoFill Destination:=Range("B1").Resize(1, Day(DateSerial(yr, i + 1, 1) - 1)), Type:=xlFillDefault
.Range("B1:AF2").HorizontalAlignment = xlCenter
.Range("B1:AF2").ColumnWidth = 7
.Range("B2") = DateSerial(yr, i, 1)
.Range("B2").NumberFormat = "d-mm"
.Range("B2").AutoFill Destination:=Range("B2").Resize(1, Day(DateSerial(yr, i + 1, 1) - 1)), Type:=xlFillDefault
End With
For Each rng In Range("B1").Resize(2, Day(DateSerial(yr, i + 1, 1) - 1))
If Weekday(rng.Value) = 1 Or Weekday(rng.Value) = 7 Then
rng.Interior.Color = RGB(8, 200, 26)
End If
Next rng
Next i
Sheets(1).Activate
End Sub