If you have code it's a good idea to post it to give anyone a starting point. When posting code, please paste within code tags (use VBA button on forum posting toolbar) to maintain indentation and readability. What you probably want to do is create a counter (For i = 1 to x) where x is the count of sheets in your workbook. That way, the counter increases as you add sheets. This may not work if you have sheets within that count that you don't want the code to affect, but it should be possible to ignore those.
Doing as you suggest is a possible approach, buy it's always better to prevent an error rather that work around it, especially when you know it's going to occur.
I have the following code that deletes Sheets "Jan" all the way to "Dec".
However, I do not have sheets from "Jul" to "Dec" yet because I do not have the data.
When I run this code, it pops up a bug because it does not have sheets from Jul to Dec. I do not want this pop up.
I need the code for the scenario;
If the following sheet does not exist, skip it and move to next command"
Sheets("Jan").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Feb").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Mar").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Apr").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("May").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Jun").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Jul").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Aug").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Sep").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Oct").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Nov").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Dec").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
I have a sheet name "Data", "Jan", "Feb", "Mar", "Apr".
I made a macro that inputs Jan ~ Dec sheet's visitors data into "Data" sheet.
I do not have May ~ Dec sheets yet.
So when I ran the macro to input the data, it shows a pop up saying;
Run-time error `9`
Subscript out of range
I know this is showing because I do not have the actual sheet for May ~ Dec yet, but could you please write a macro that does something like, "If the sheet doesn't exist, skip and move to next" ?
I want to keep this macro that analyzes from Jan ~ Dec, but doesn't want to show that pop up when it is missing certain month's sheet and just skips to next.