+ Reply to Thread
Results 1 to 12 of 12

Runtime error 9 "subscript out of range"

  1. #1
    Registered User
    Join Date
    08-29-2013
    Location
    Cambridge, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    4

    Runtime error 9 "subscript out of range"

    Hi I am trying to automate a process from which i copy a sheet ("NA") from a workbook (released daily with date as its name) and paste it into an existing workbook ("Overflow Log.xlsx"). Here is the code I have so far.

    Please Login or Register  to view this content.
    The code opens the existing file (overflow log) without any issues but fails at "ThisWorkbook.Sheets("NA").Copy After:=wbOpen.Sheets("Graph")" giving me Runtime error 9 "subscript out of range". Note: the copied sheet needs to go after or before a sheet named "Graph" in the Overflow Log workbook.

    any help will be appreciated. Thanks
    Last edited by Frustratedemployee; 08-29-2013 at 04:20 PM.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Runtime error 9 "subscript out of range"

    Please Login or Register  to view this content.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Runtime error 9 "subscript out of range"

    hi Frustratedemployee, welcome to Excelforum, this error means that either you do not have sheet "Graph" in the destination workbook (or it is named differently) or there is no sheet "NA" (or it is named differently)

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Runtime error 9 "subscript out of range"

    @ watersev

    Just out of curiosity, won't opening the wb make it active? After its active will thisworkbook behave as normal?

  5. #5
    Registered User
    Join Date
    08-29-2013
    Location
    Cambridge, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Runtime error 9 "subscript out of range"

    Thanks for your response gentleman, but its still a no go. I have read in some other threads, where it was suggested that the sheet name I am using in my code may not exist or may have a space before or after., so I had already made sure of that. See below
    Capture1.JPGCapture2.JPG.

    Maybe I am missing something else with the naming of the sheets? Also, with XeRo Solus's code the error changed to THIS.JPG
    Last edited by Frustratedemployee; 08-29-2013 at 05:16 PM.

  6. #6
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Runtime error 9 "subscript out of range"

    chart type? Is this included with other code?

    Can you upload your workbooks?

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Runtime error 9 "subscript out of range"

    @Xero Solus
    yes opening the workbook makes it active but it does not become thisworkbook

    @Frustratedemployee

    Try this if you will get any error and on what line

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-29-2013
    Location
    Cambridge, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Runtime error 9 "subscript out of range"

    I have uploaded both workbooks.

    @ watersev...your code gave the same error on line
    Set main_sh = ThisWorkbook.Sheets("NA")

    Thanks for your help.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Runtime error 9 "subscript out of range"

    What is the error?

    If to amend file name to Overflow Log2.xlsx in your code everything works as expected on the posted files

  10. #10
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Runtime error 9 "subscript out of range"

    try this instead:

    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Runtime error 9 "subscript out of range"

    Is your code residing in a personal macro workbook? Then it should be (amend file name if required):

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    08-29-2013
    Location
    Cambridge, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Runtime error 9 "subscript out of range"

    The code worked flawlessly. Thank you Gentleman, for your help. my code does reside in Personal.xlsb

+ 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. Runtime error 9 "subscript out of range"
    By eLakmal in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-08-2013, 10:09 PM
  2. [SOLVED] Runtime error 9 "subscript out of range"
    By MyCousinVinnie in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-08-2012, 11:28 PM
  3. Error msgs: "Object varible or with block variable not set"; "subscript out of range"
    By menyanthe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-26-2009, 04:58 PM
  4. [SOLVED] "Subscript out of range" error for: Workbooks("Test1.xls").Save
    By Just12341234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2005, 11:05 AM
  5. [SOLVED] FileCopy Command Giving "Subscript Out of Range" Error Message
    By Jim Hagan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2005, 02:05 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