+ Reply to Thread
Results 1 to 11 of 11

Subscript Out Of Range Error

  1. #1
    Forum Contributor
    Join Date
    08-06-2012
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    119

    Subscript Out Of Range Error

    I have a large workbook with several macros that refreshes all of the data for a specific year. I have some code on my Dashboard that calls the main Refresh macro when a specific cell is changed on my Dashboard sheet. If I just click the run button in the VBA editor for the main Refresh macro, the code runs perfectly with no problem every time. However, if I run the macro from the code on the Dashboard sheet, it (sometimes) errors out with a Subscript out of range error. Sometimes it runs fine all the way through, and sometimes it errors out, seemingly randomly.

    It always errors out on this line:
    Please Login or Register  to view this content.
    Here is my code on the Dashboard sheet:
    Please Login or Register  to view this content.
    And here is my main code:
    Please Login or Register  to view this content.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Subscript Out Of Range Error

    When it errors, what values do the variable (Bookname and NextSheet) have?

    Does the existing sheet exist? Does the new Sheet number NOT exist?

    I suspect that when you run from the main Refresh macro, you maybe do some preparation that hasn't been done elsewhere.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    08-06-2012
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    119

    Re: Subscript Out Of Range Error

    Thanks for the reply!

    The values of those two variables vary (haha) between refreshes.

    When I hover over BookName, or NextSheet in break mode they will give me the proper values.

    The sheet does exist - it will say like Sheet59, or Sheet138 when I hover over VBComponents(Sheets(BookName).CodeName).

    And I really can't think of one thing I do differently to prepare the sheet when I run directly from the main Refresh macro.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Subscript Out Of Range Error

    I'm not sure of the purpose of the code, however, if I understand correctly, it is changing the Code Name of a named worksheet. Therefore, the worksheet name as defined by the Bookname variable MUST exist. Equally, the Code Name represented by "Sheet" and the variable NextSheet DOES NOT exist.

    So, for example, this test works for me with a worksheet named "locations" where the Code Name Sheet50 does NOT exist

    Please Login or Register  to view this content.

    So, if it fails, I'm guessing one or both of those conditions are not true.


    Regards, TMS

  5. #5
    Forum Contributor
    Join Date
    08-06-2012
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    119

    Re: Subscript Out Of Range Error

    The purpose of the code is indeed to change the Code Name (the name of the sheet it shows in the VBA Editor). It does this because every time it refreshes it is deleting and adding up to 20 new worksheets. It just failed for the first time today. I swear I ran it through from the Dashboard code at least 25 or more times and it worked flawlessly.

    However, it just failed. Im attaching a screen shot with some watches showing both that BookName does exist (blue), and NextSheet (green) does not exist.

    Screen Shot.jpg

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Subscript Out Of Range Error

    Well, that seems to meet my expectations.

    Can I ask why you feel the need to do this? On the basis that you delete all the additional worksheets, that is with an index greater than 5, what's the point of renumbering them? What would happen if you did not renumber them? Has it caused a problem in the past (NOT renumbering them, that is)

    Regards, TMS

  7. #7
    Forum Contributor
    Join Date
    08-06-2012
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    119

    Re: Subscript Out Of Range Error

    I want to do this for several reasons:
    1. Even after deleting the sheets the VBA editor will continue to number the sheets sequentially so if I refresh this several times, its quickly enters the 100's and it becomes cluttered and difficult to navigate inside the VBA editor.
    2. Since the sheet names (not the code names) change each time without any predictability, it is difficult to reference specific sheets in other code. For example, I can't use Sheet7.Activate, I can't use Sheets(7).Activate and I can't use Sheets("WhateverName").
    3. I have some formulas that calculate a row number based on their sheet number (I'm not positive the SHEET() formula refers to the code name or just position of the sheet from left to right in the workbook). EX:
      • So this cell on Sheet6 grabs the contents of cell O2 on the Stock Query sheet, Sheet 7 would get O3... and so on.
      Please Login or Register  to view this content.
    4. Finally, I know its unlikely that I would reach it, but there is an upper limit of sheets allowed in the VBA editor.

  8. #8
    Forum Contributor
    Join Date
    08-06-2012
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    119

    Re: Subscript Out Of Range Error

    Actually, it looks like I can still refer to specific sheets using Sheets(i). That gave me an idea. I just removed that section of the code from the For loop that creates the new sheets and put it into its own loop at the end with an error handeler, and that seems to have fixed the problem *fingers crossed*. Here is my code now:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Subscript Out Of Range Error

    I'm glad you have a solution.

    Note that you don't have to activate a worksheet to delete it.

    So this:

    Please Login or Register  to view this content.

    becomes this:

    Please Login or Register  to view this content.

    And this:

    Please Login or Register  to view this content.

    could probably be replaced by:

    Please Login or Register  to view this content.

    Regards, TMS
    Last edited by TMS; 02-12-2015 at 04:27 PM. Reason: Mistook DisplayAlerts for ScreenUpdating

  10. #10
    Forum Contributor
    Join Date
    08-06-2012
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    119

    Re: Subscript Out Of Range Error

    Thanks for the suggestions! I knew the first one (I don't know why I was activating the sheet before I deleted it), but I didn't know the second one!

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Subscript Out Of Range Error

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Run-time error '9': subscript out of range - error occurs on multiple computers except one
    By BrettE in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-06-2014, 11:19 PM
  2. Defining Array - Runtime error 9, Subscript out of range error
    By MaartenW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-03-2012, 07:32 AM
  3. Runtime Error - Subscript out of range despite On Error statement
    By DoctorG in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2006, 11:05 AM
  4. Type Mismatch error & subscript out of range error
    By Jeff Wright in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2005, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1