+ Reply to Thread
Results 1 to 5 of 5

Subscript out of Range Error with using Worksheets.Select on an existing Worksheet

  1. #1
    Registered User
    Join Date
    01-16-2014
    Location
    Hannover, Germany
    MS-Off Ver
    Excel 2007
    Posts
    3

    Subscript out of Range Error with using Worksheets.Select on an existing Worksheet

    Hello,

    in my workbook, I have a hidden worksheet, in which I set some ranges to appear in a diagram. I make the sheet visible, select the worksheet, set the ranges, and put them as a series in my diagram.
    But when I look at the error variable (which I did to find out that I need to make the sheet visible), I get an "subscript out of range" error. The sheet is existing (otherwise I couldn't make it visible), I can set the ranges, and my code works, but I want to know what's wrong here. Thanks in advance!

    Here a snippet of my code:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Subscript out of Range Error with using Worksheets.Select on an existing Worksheet

    Hi and welcome to the forum,
    When you are working with range and cells and different worksheets, it is always good practice to identify the sheets before the range and the cells:

    For example :
    Please Login or Register  to view this content.

    or simpler :
    Please Login or Register  to view this content.
    Otherwise the range that you define will be on the activesheet at the moment the line of code is executed.
    If you have multiple sheets in your workbook, that can cause some problems.

    And if you do this, you should not need to make the sheet visible to define your range.
    Cheers
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Registered User
    Join Date
    01-16-2014
    Location
    Hannover, Germany
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Subscript out of Range Error with using Worksheets.Select on an existing Worksheet

    I will try the second option, and see if that works. Nicer than mine anyway (which was working in earlier versions with Worksheets.Select, but clean code is preferable).

    But any idea, why Excel first sets the worksheet visible, so is finding my worksheet, and than seems to lose contact, to find afterwards the right data on the selected worksheet? Do I activate the sheet while making it visible?

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Subscript out of Range Error with using Worksheets.Select on an existing Worksheet

    I can not reproduce your problem. I just tried your code and the "Coordinates" sheet is visible, selected and then find function works. ??!?
    Can you post your file?

  5. #5
    Registered User
    Join Date
    01-16-2014
    Location
    Hannover, Germany
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Subscript out of Range Error with using Worksheets.Select on an existing Worksheet

    I've tried with a blank excel workbook, as I cannot post the original file, and just have put the code, where I set visibility of the worksheet "Coordinates", and afterwards select it.

    Making the worksheet visible leaves the internal error set to 0 (so no error), selecting it afterwards results in error 9 (subscript out of range). I can override that with "On Error Resume Next", I know that I get the right data in my original sheet, I can do nicer programming avoiding the Select-Method - but I want to understand, why Excel-VBA is throwing there an error.
    Attached Files Attached Files

+ 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] Subscript Out Of Range Error on Improting Excel Worksheet to Access
    By Aceso in forum Access Tables & Databases
    Replies: 2
    Last Post: 01-31-2013, 10:08 PM
  2. [SOLVED] Select Sheets based on variable array -ERROR 9 Subscript out of range
    By airmiles in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2012, 07:28 PM
  3. Subscript out of range error when trying to select a range
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2010, 07:57 PM
  4. Trying to determine if worksheet exists, subscript out of range error
    By js999 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2007, 12:03 PM
  5. [SOLVED] Cannot activate worksheet using VBA. "Subscript Out of Range" error.
    By Cloudfall in forum Excel General
    Replies: 15
    Last Post: 02-10-2006, 09:40 AM

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