I have an excel workbook with worksheets named in the date format "mmm-yy".
my last worksheet on the right is named "Nov-16"
I have a macro to copy the last sheet and add a month to the date. Eg. the new sheet will be named Dec-16.
For some reason it named Dec-17, clicking the marco once more names the new sheet Jan-18 (this should have been Jan-17).
My code is as follows:
my last worksheet on the right is named "Nov-16"
I have a macro to copy the last sheet and add a month to the date. Eg. the new sheet will be named Dec-16.
For some reason it named Dec-17, clicking the marco once more names the new sheet Jan-18 (this should have been Jan-17).
My code is as follows:
Code:
Sub NewMonthSheet()
Dim lSht As Worksheet
Dim nSht As Worksheet
Dim shName As String
Set lSht = Sheets(Sheets.Count)
If IsDate(lSht.Name) Then
shName = Format(DateAdd("m", 1, lSht.Name), "mmm-yy")
On Error Resume Next 'Tests that sheet doesn't already exist
Set nSht = Sheets(shName)
On Error GoTo 0
If nSht Is Nothing Then
lSht.Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = shName
Else
MsgBox "Sheet """ & shName & """ already exists!" _
, vbCritical
End If
Else
MsgBox "Last sheet name does not" & Chr(10) _
& "represent a month!", vbCritical
End If
End Sub
Last edited: