+ Reply to Thread
Results 1 to 25 of 25

Error code "run time error 9: subscript out of range

  1. #1
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Error code "run time error 9: subscript out of range

    Hello all!
    I am having the following problem
    "Set wsh = ThisWorkbook.Worksheets(sh.Cells(2, i).Value)"

    I have included below the full code:
    Please Login or Register  to view this content.
    Any help would be greatly appreciated!

    Thank you

  2. #2
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    Or if anyone can link me to a forum post which shows how to fix this error then that would be appreciated.

    Thank you so much.

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

    Re: Error code "run time error 9: subscript out of range

    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
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    I have just tried that instead of "Set wsh = ThisWorkbook.Worksheets(sh.Cells(2, i).Value)" and it did not work

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Error code "run time error 9: subscript out of range


    Hi,

    try Text property rather than Value and far above all check the cell content and if a worksheet exists accordingly !

  6. #6
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    Could you give me a sample please?
    Just so I know i'm going to do the right thing

    Thank you

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

    Re: Error code "run time error 9: subscript out of range

    See Top banner regarding sample file...

  8. #8
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,026

    Re: Error code "run time error 9: subscript out of range

    Are you sure that a worksheet exists that is named as whatever sh.Cells(2, i).Value is?

  9. #9
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    I really am not to sure, it just came across my desk this morning. And this was the first problem with it

  10. #10
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    Sheet 2 is an admin board

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

    Re: Error code "run time error 9: subscript out of range

    Look at this sample....See what i mean...
    Please Login or Register  to view this content.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    Yeah I know what you mean. Just still dont know the method to fix it.

    Its baffling me haha

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

    Re: Error code "run time error 9: subscript out of range

    Then upload your sample file...Either should work...
    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,026

    Re: Error code "run time error 9: subscript out of range

    With this line
    "Set wsh = ThisWorkbook.Worksheets(sh.Cells(2, i).Value)"

    if sh.Cells(2, i).Value isn't the name of any worksheet then you get the error you describe.

  15. #15
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    Which sample file? I am getting a bit muddled up now -.-

    Is it now the file I have attached above?

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

    Re: Error code "run time error 9: subscript out of range

    Look at all the comments...Are you sure a sheet with this name is present...

  17. #17
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    I have attached the file below which should be linked.
    Which is called Sheet1 (User Management)
    Attachment 734496

  18. #18
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    I have just set it to this;
    Set wsh = ThisWorkbook.Sheets("User Management")
    And it is giving me an error on this line now.
    wsh.Visible = xlSheetVeryHidden

    Or is the first line wrong?

  19. #19
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,305

    Re: Error code "run time error 9: subscript out of range

    eighteen posts !!!!!!!!!!!!!!!!
    look at the big yellow banner and upload your workbook.
    crystal ball gazing and guessing wastes time.
    torachan.

  20. #20
    Registered User
    Join Date
    05-28-2021
    Location
    UK
    MS-Off Ver
    10
    Posts
    13

    Re: Error code "run time error 9: subscript out of range

    I have attached code and document. Sorry about that!

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

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

    Re: Error code "run time error 9: subscript out of range

    Nope...That upload has no reference to your actual code...
    No User Management Sheet etc etc...You are not going to get results here if you cannot be more transparent...
    Last edited by sintek; 05-28-2021 at 10:33 AM.

  22. #22
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,305

    Re: Error code "run time error 9: subscript out of range

    what you have just posted does not seem to have any relevance to your opening posts.
    however I have only looked at the last posting in any detail.
    it would appear that if a password 'x' is entered you want the sheet to be invisible ?????
    this would not work with your attached file for the reason that there has to be at least one visible sheet - you only have one sheet so the process would fail.
    Also the cell referencing in the code did not make sense - you had rows confused with columns - in the attached there is an added empty sheet - if you put a password 'x' in the sheet1 will then become invisible - I do not pretend to follow your logic for wanting to perform this action.
    torachan.
    Attached Files Attached Files

  23. #23
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Error code "run time error 9: subscript out of range

    Hi there,

    There was a lot of mind-reading involved, but take a look at the attached version of your workbook and see if it gets you moving in the right direction. It uses the following code:

    Please Login or Register  to view this content.
    The highlighted values may be altered to suit your requirements.

    Note: I had to change your "accented" letters to normal ones so that the code could be uploaded here.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  24. #24
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Error code "run time error 9: subscript out of range

    Hi again,

    Thanks for your private message.


    Thank you for your help.. Which file do I add this to?

    It's not really possible for me to say as I don't have access to your workbook(s?). Remember that I had to do a lot of mindreading before I came up with this suggestion.

    Your requirement seemed to be to provide a facility for displaying/hiding and protecting/unprotecting various worksheets depending on the identity and/or category of the User. The workbook I posted meets such a requirement, so maybe the most straightforward thing for you to do is to take your various "real" worksheets and copy them into the workbook I posted, rather than trying to put some or all of my workbook into the ones you have.

    We can talk more about this if you need.

    Regards,

    Greg M

  25. #25
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Error code "run time error 9: subscript out of range

    Hi again,

    Thanks for your private messages about this - glad to hear that things seem to be working the way you want now.

    As discussed, I'm posting a sanitised version of your workbook so that anyone who might have been following this thread can see the solution we arrived at.

    I was very pleased that you considered my assistance worthy of the donation you made to The Dogs Trust.

    Regards,

    Greg M
    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] Why my code is producing Error "SUBSCRIPT OUT OF RANGE" While using variant arrays?
    By Asad Mir in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-20-2020, 10:22 AM
  2. [SOLVED] Help with error run-time error '9" (subscript out of Range)
    By thong127 in forum Excel General
    Replies: 7
    Last Post: 05-02-2018, 02:17 PM
  3. "Run-time error '9': Subscript out of range" in some computers!
    By m.zaeim in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-14-2016, 04:03 PM
  4. "Subscript out of range error" in a simple code
    By Mutak in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-30-2016, 04:35 PM
  5. "Run-time error '9': Subscript out of range" when referencing between workbooks
    By roberts23 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-08-2014, 10:33 AM
  6. Error "Subscript out of range", cant seem to find where code is wrong
    By KeithMale in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-14-2013, 07:07 PM
  7. [SOLVED] trying to understand why a code is not working "error 9 subscript out off range"
    By cdafonseca in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-09-2013, 12:33 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