+ Reply to Thread
Results 1 to 4 of 4

Subscript out of range (Error 9)

  1. #1
    Registered User
    Join Date
    11-14-2017
    Location
    Brazil
    MS-Off Ver
    2007
    Posts
    8

    Subscript out of range (Error 9)

    Hello,

    I have a worksheet that has the names of the employees in the area of the company I work in, called Employees. Each employee has their own worksheet (the name of the sheet is the name of the employee). The purpose of the for looping is to search the Employees worksheet for employee names and make changes to their respective worksheets if a given condition is met. But Excel returned with the error Subscript out of range (Error 9). Why is this error ocurring? The error is in the red line of the code.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Subscript out of range (Error 9)

    Have you checked the sheet names and the values in the list for things like leading/trailing spaces, spelling errors etc?

    Also, what workbooks are open when you run the code?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Subscript out of range (Error 9)

    Not to sure without seeing all your code, but this stands out...
    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  4. #4
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Subscript out of range (Error 9)

    Quote Originally Posted by carolmanente View Post
    Why is this error occurring?
    Subscript out of range happens when you try and refer to an element of a collection/array that does not exist. The gist of it as it pertains to your code is you are trying to .select a sheet that doesnt exist.

    To overly simplify, if you have 3 sheets in a file and did:

    Please Login or Register  to view this content.
    you should get the same error. If your sheets are named: Sheet1, Taxes, Bob and you did:

    Please Login or Register  to view this content.
    you should also get the error.

    you need to check what:

    Please Login or Register  to view this content.
    becomes in that line when you get the error. You could add a debug line above it and have it debug.print to the immediate window the value of just """ & Plan2.Cells(i, 3).Value" to see what it is at that stage in the loop prior to failing. Alternatively you could add a break and step through the code and evaluate the value prior to getting the error.

    Since we dont know whats in your cells I cant speak to why you have "" & preceding the cell values, not sure what the benefit of adding nothing before that string would be.

    Its also generally asking for trouble to have a with inside another with. It should work, but often gets confused the more closely related both objects are. Especially when combined with implied language like Sheets() instead of workbook.worksheets() or .cells instead of sheet/worksheet.cells().

    without sample data/workbook its hard to be more specific about the cause of your problem
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

+ 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