'ON ERROR GOTO' not working inside '-FOR NEXT' loop

plwhittington

Board Regular
Joined
May 10, 2007
Messages
132
I have a worksheet (CurSht) that has date rangess listed in rows and I want to create new sheets with the date range being the new sheet name if that sheet does not exist already. I get a RUNTIME ERROR 9 - SUBSCRIPT OUT OF RANGE error when running the code when it gets to the SELECT statement to select the sheet. (all variables have valid values when the error occurs). I tried to use the ON ERROR GOTO command to create the sheet if it doesnt exist, but it ignores it. Not sure if I need a RESUME statement, but can't get it to work. Thanks for any suggestions. Below is the code.


For RowNum = 12 To LastRow
Sheets(CurSht).Select

.....

On Error GoTo DataShtDoesntExists:
Sheets(Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay).Select '******* RUNTIME ERROR 9 HERE
Sheets(Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay).Cells.Clear
GoTo DataShtAlreadyExists:
DataShtDoesntExists:
Sheets.Add.Name = Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay
DataShtAlreadyExists:

Next RowNum
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Puertorekinsam

Active Member
Joined
Oct 8, 2005
Messages
293
try something like this :
obviously you need to define your variables
Code:
    Sub sheetname_Array()
    
    Dim ws As Worksheet
    Dim SheetsArray() As String
    ReDim SheetsArray(0)
    For Each ws In ActiveWorkbook.Worksheets
        SheetsArray(UBound(SheetsArray)) = ws.Name
        ReDim Preserve SheetsArray(1 + UBound(SheetsArray))
        
    Next
    ' I believe SheetToLookFor is Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay
    SheetToLookFor = Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay
    If IsInArray = (UBound(Filter(SheetsArray, SheetToLookFor)) > -1) Then
        Sheets(Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay).Cells.Clear
    Else
        Sheets.Add.Name = Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay
    End If


End Sub
 
Upvote 0

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,072
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
From the limited portion of code you've posted, it's not clear why your GoTo isn't working. As a test, place this line just before the GoTo line:

MsgBox Err.Number

what do you get in the message box when you run the code?
 
Upvote 0

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
7,995
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Try removing colon marks shown in RED

Rich (BB code):
On Error GoTo DataShtDoesntExists:


Sheets(Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay).Select '******* RUNTIME ERROR 9 HERE
Sheets(Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay).Cells.Clear


GoTo DataShtAlreadyExists:


DataShtDoesntExists:
Sheets.Add.Name = Yr & Mnth & BegDay & "-" & Yr & Mnth & EndDay
DataShtAlreadyExists:

Dave
 
Last edited:
Upvote 0

plwhittington

Board Regular
Joined
May 10, 2007
Messages
132
Puertorekinsam - the variables are defiuned in the columns. I took the code you provided was able to accomplish what I needed. Thank you for your help.

JoeMo- the error code was 9

DMT32- tried that solution first, but still encountered the error. Thanks anyway for the suggestion.
 
Upvote 0

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,072
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Puertorekinsam - the variables are defiuned in the columns. I took the code you provided was able to accomplish what I needed. Thank you for your help.

JoeMo- the error code was 9

DMT32- tried that solution first, but still encountered the error. Thanks anyway for the suggestion.
That's your problem, you already have an error in your code coming into the GoTo. You need to deal with that error (i.e clear it) before you execute the GoTo line or, as you have observed, the GoTo line will be ignored. You might want to post all of your code if you have problems correcting that.
 
Upvote 0

Forum statistics

Threads
1,191,607
Messages
5,987,637
Members
440,104
Latest member
thigarette

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top