• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Add sheets- all months

Hi,

Can you please let me know why this macro is deleting existing sheets when I run it?





Code:
Sub AddSheetsWithAllMonths()
'Creates 12 new sheets with months name & current year
    Dim J As Integer
    Dim k As Integer
    Dim sMo(12) As String

    sMo(1) = "January" & " " & Year(Now)
    sMo(2) = "February" & " " & Year(Now)
    sMo(3) = "March" & " " & Year(Now)
    sMo(4) = "April" & " " & Year(Now)
    sMo(5) = "May" & " " & Year(Now)
    sMo(6) = "June" & " " & Year(Now)
    sMo(7) = "July" & " " & Year(Now)
    sMo(8) = "August" & " " & Year(Now)
    sMo(9) = "September" & " " & Year(Now)
    sMo(10) = "October" & " " & Year(Now)
    sMo(11) = "November" & " " & Year(Now)
    sMo(12) = "December" & " " & Year(Now)

    For J = 1 To 12
        If J <= Sheets.Count Then
            If Left(Sheets(J).Name, 5) = "Sheet" Then
                Sheets(J).Name = sMo(J)
            Else
                Sheets.Add.Move After:=Sheets(Sheets.Count)
                ActiveSheet.Name = sMo(J)
            End If
        Else
            Sheets.Add.Move After:=Sheets(Sheets.Count)
            ActiveSheet.Name = sMo(J)
        End If
    Next J

    For J = 1 To 12
        If Sheets(J).Name <> sMo(J) Then
            For k = J + 1 To Sheets.Count
                If Sheets(k).Name = sMo(J) Then
                    Sheets(k).Move before:=Sheets(J)
                End If
            Next k
        End If
    Next J

    Sheets(1).Activate
    MsgBox "I am done!", vbInformation, "Do It Fast!"
End Sub
 
paulcherianc
Run that macro step-by-step
and
You would see
what is going on there.
(Especially, WHERE those new sheets would create.)

ps. If You are also sorting those sheets then use 'yyyy mmm' format.
 
Back
Top