There are multiple sheets/tabs in this workbook.So how many sheets are in this workbook exactly?
Do you want to rename ALL the prior worksheets?
At first I thought having that scenario alone, I can just change the name of sheet/tab's name and repeat the code all the way til december... :'(From Post #1: "If sheet name "Jan" exists, rename this sheet as "Feb"."
Now you say that it could be any month of the year.
Sub SaveActiveSheet()
Dim X As Variant, Months As Variant
Dim I As Long
Dim M As String, ShList As String
Dim NewName As Boolean
Months = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
ShList = VBA.Join(Months, "!")
For I = 0 To UBound(Months)
NewName = False
X = vbNullString
M = Months(I)
On Error Resume Next
X = ActiveWorkbook.Worksheets(M).Name
On Error GoTo 0
If X = vbNullString And InStr(ShList, ActiveSheet.Name & "!") = 0 Then
Select Case ActiveSheet.Name
Case "Main", "Summary" '<- sheets to be ignored
Case Else
ActiveSheet.Name = M
NewName = True
Exit For
End Select
End If
Next I
If Not NewName Then
MsgBox "All Months are taken!", vbCritical
End If
End Sub
Thank you so much!!!!Without knowing the larger picture, this is a bit of a guess.
VBA Code:Sub SaveActiveSheet() Dim X As Variant, Months As Variant Dim I As Long Dim M As String, ShList As String Dim NewName As Boolean Months = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec") ShList = VBA.Join(Months, "!") For I = 0 To UBound(Months) NewName = False X = vbNullString M = Months(I) On Error Resume Next X = ActiveWorkbook.Worksheets(M).Name On Error GoTo 0 If X = vbNullString And InStr(ShList, ActiveSheet.Name & "!") = 0 Then Select Case ActiveSheet.Name Case "Main", "Summary" '<- sheets to be ignored Case Else ActiveSheet.Name = M NewName = True Exit For End Select End If Next I If Not NewName Then MsgBox "All Months are taken!", vbCritical End If End Sub
Sub Add_Next_Month()
Dim ListArray
Dim i As Long
ListArray = Application.GetCustomListContents(3)
For i = LBound(ListArray, 1) To UBound(ListArray, 1)
On Error Resume Next
If IsEmpty(ActiveWorkbook.Sheets(ListArray(i)).Name) Then ThisWorkbook.Sheets.Add(, Sheets(Sheets.Count)).Name = ListArray(i): Exit For
On Error GoTo 0
Next i
End Sub