+ Reply to Thread
Results 1 to 8 of 8

Help with subscript out of range error

  1. #1
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Help with subscript out of range error

    Hi all,

    I have the following code to create copies of my 4 template sheets to create a sheet for each month. It seems when the loop gets around to creating a copy of the template for the second time it doesn't like it. I can't figure out why or a solution to work around it. So when it gets to "Sheets("Jan" & vName).Copy after:=Sheets(i2)" to create the first March sheet, I get subscript out of range. Any ideas?

    Please Login or Register  to view this content.

    Thanks!

  2. #2
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    419

    Re: Help with subscript out of range error

    Not sure if this will fix your problem, but I can tell you that Sheet.Visible will never be True, as it is not a Boolean. Try replacing with this and see how you go:


    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: Help with subscript out of range error

    Questions that arise: where is the variable i2 defined (dimmed)? Where does it have its value set? How do you think the function visible_sheets works?

    Do you have global variables for i and i2? Are they set in another module/subroutine? A function returns a value through the function name, as a general rule.

  4. #4
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Help with subscript out of range error

    @mgs73 - When calling the function, I'm able to get the correct number of visible sheets, so that is not the problem.

    @InvisibleMan - I've changed a fair amount of the code at this point so it's tough to answer your questions.

    I think a better question for both of you would be once I have the function visible_sheets, how do I return that number in my subroutine? The portion "after:= Sheets(i2)" doesn't work because I can't seem to return the value from visible_sheets.

  5. #5
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: Help with subscript out of range error

    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: Help with subscript out of range error

    To be fair, I don't really understand the relevance of visible sheets. You could just put the new sheet after Sheets(Sheets.Count)

  7. #7
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Help with subscript out of range error

    You have to define i2 or don't use it.

    Maybe you want the below

    Please Login or Register  to view this content.
    then use it like this

    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    419

    Re: Help with subscript out of range error

    Can you upload a workbook? It is likely an easy fix, but too difficult to guess what it might be.

    +1 to everything InvisibleMan said. Do you have 'Option Explicit' at the top of your code?

+ 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. Runtime Error - Subscript out of Range / Object Error
    By JHRice in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2012, 05:14 PM
  3. 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
  4. 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
  5. Subscript out of range error - save copy error
    By bg18461 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2006, 11:53 AM
  6. 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