0

I'm new to Excel VBA and started to build a time tracking workbook for learning. A part of that is a For-Loop which shall add one named worksheet for every month in a year:

Sub newyear()

Dim month(12) As String
Dim i As Integer

month(1) = "Januar"
month(2) = "Februar"
...
month(12) = "Dezember"

For i = 1 To 12
On Error Resume Next
Sheets.Add(Tabelle1).Name = month(i)
MsgBox Err.Number    <- this throws Error 9: "Subscript Out Of Range" after
                        every worksheet added during the loop
Next i

End Sub

During Runtime while the loop is adding worksheet after worksheet the MsgBox pops up after every single added sheet with Error 9: "Subscript Out Of Range".

I don't know why this is happening, started reading up quite a bit on the web and still have no solution.. maybe I'm missing something basic, because I'm a beginner.

Please help me.

Thanks in advance!

1
  • Please mark as the solution if my code fits with you Commented Mar 31, 2016 at 10:23

2 Answers 2

2

Please see the below code.

Sub newyear()
On Error Resume Next
Dim month(12) As String
Dim i As Integer
month(1) = "January"
month(2) = "February"
month(3) = "March"
month(4) = "April"
month(5) = "May"
month(6) = "June"
month(7) = "July"
month(8) = "August"
month(9) = "September"
month(10) = "October"
month(11) = "November"
month(12) = "Dezember"
Dim ws As Worksheet
For i = 1 To 12
    With ThisWorkbook
        If Worksheets(month(i)).Name <> month(i) Then
            Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
            ws.Name = month(i)
        End If
    End With
    If Err.Description <> "" Then
        Err.Clear
        'do what you want to do 
    End If
Next i
End Sub
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the suggestion! Sadly after adding 'MsgBox = Err.Number' before 'Next i' to your code Error 9 ist still popping up after each added sheet :(.
@Fr4gFr0g, May i know why you adding Err.Number before Next i
@Fr4gFr0g, edited the above answer. Pls mark as solution if its correct.
Okay, so initially I wanted the loop to function correctly, even if sheets for some months already exist. When that's the case the loop adds an unnamed sheet for every existing one <- I wanted to detect this by checking for error like this If Err.Description <> "" Then activesheet.delete (pseudo code). The Problem is that the named sheets also get deleted because for every sheet added (even if it's not already existing) Error 9 pops up.. and sadly, with your solution this Error 9 still happens.
Well.. this just clears the Error. It still doesn't prevent it from happening in the first place.
|
0

I am not sure why you want to avoid having an error. You can probably make a longwinded code for that, but there no use for it. You can also try something shorter:

Sub newyear()

   Dim month(12) As String
   Dim i As Integer

   month(1) = "January"
   month(2) = "February"
   month(3) = "March"
   month(4) = "April"
   month(5) = "May"
   month(6) = "June"
   month(7) = "July"
   month(8) = "August"
   month(9) = "September"
   month(10) = "October"
   month(11) = "November"
   month(12) = "Dezember"

   On Error Resume Next
   For i = 1 To 12
       If Worksheets(month(i)) Is Nothing Then
           Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = month(i)
       End If
   Next

End Sub

2 Comments

Yeah.. well that's the conclusion I also cam to: Why actually try to avoid the error?! But, you know, I thought when my code throws an error than obviously I coded something wrong, no? I thought the goal is to write clean code and naive me thought clean code shouldn't generate errors. Whatever thanks for the input :).
Sometimes an error is not a problem, but a feature :) In VBA, as in Excel, you can use errors to your benefit. It's a good shortcut for many cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.