+ Reply to Thread
Results 1 to 6 of 6

Run Time Error 9: Subscript Out of Range when Copy and Paste between Workbooks

  1. #1
    Registered User
    Join Date
    11-05-2016
    Location
    U.S.
    MS-Off Ver
    Office 365 Excel 2016
    Posts
    6

    Run Time Error 9: Subscript Out of Range when Copy and Paste between Workbooks

    Hello,

    I'm not finished, but I'm trying to modify code that will copy data 600 rows at a time and then paste it in a new workbook each time. I will eventually try to make it name each workbook with an incremental number after the file name, or I will just let the user type the file name in an input box like I have below.

    Right now I get Run Time Error 9: Subscript Out of Range on the line below:
    Please Login or Register  to view this content.
    (I've also tried using
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    as in the original code.)

    Question: 1) What is the issue b/c I've looked at code that uses Range.Copy Destination: between workbooks:

    Do I need to activate the nrenwb with nrenwb.Activate? I had to add
    Please Login or Register  to view this content.
    b/c I got the same Run Time Error 9 highlighting the same line above. As I say, I don't see why b/c won't
    Please Login or Register  to view this content.
    activate the nrenwb workbook?

    2) Also, I've seen on the internet that if you have a path in the filename you can get this error, but
    Please Login or Register  to view this content.
    makes sure the variable, nrenwb, just gives the filename entered by the user with '.csv', doesn't it? Though in the locals window the Full Name does have a path.

    I would appreciate any help debugging this error. Thank you.

    Please Login or Register  to view this content.
    Source: http://stackoverflow.com/questions/3...-workbook-loop

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Run Time Error 9: Subscript Out of Range when Copy and Paste between Workbooks

    Untested but try change to
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-05-2016
    Location
    U.S.
    MS-Off Ver
    Office 365 Excel 2016
    Posts
    6

    Re: Run Time Error 9: Subscript Out of Range when Copy and Paste between Workbooks

    Hello jindon,

    Thank you for your reply. I tried using the code and it still stops on the same line with the same error even after you changed it to Sheets(1):
    Please Login or Register  to view this content.
    I would appreciate any input from you or others to help debug. Thank you, again.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Run Time Error 9: Subscript Out of Range when Copy and Paste between Workbooks

    After less than a minute scan...

    Is nrenwb a csv file? If it is then Excel has no idea where A1 is. Actually if it is a csv file it doesn't even have sheets.
    Could that be the problem?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Run Time Error 9: Subscript Out of Range when Copy and Paste between Workbooks

    It might be
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    11-05-2016
    Location
    U.S.
    MS-Off Ver
    Office 365 Excel 2016
    Posts
    6

    Re: Run Time Error 9: Subscript Out of Range when Copy and Paste between Workbooks

    I think both of you answered it. It works now after moving the SaveAs to CSV to the end *after* it goes back and forth between the owb and nwb workbooks to copy and paste.

    I hadn't also changed the variable name, nrenwb, back to nwb as jindon suggested. That line was trying to find a named variable, nrenwb, that it had't created.

    Changes made:

    1) I removed references to the nrenwb variable completely.

    Also, off subject, but having to do with this code.

    2)
    Please Login or Register  to view this content.
    works better for what this code tries to do, so I also removed the
    Please Login or Register  to view this content.
    .

    3) I also moved the InputBox to just before the Save As code.


    Please Login or Register  to view this content.
    I really appreciate both of you helping. Thank you!

    P.S. Just FYI, for anyone wanting to copy and paste into a new workbook each time as mentioned in the original post, this code isn't ready for that yet. It just copies 10 rows at a time into column A of a new workbook so that the data is pasted underneath the last 10 rows of pasted data.
    Last edited by excel989; 04-27-2017 at 02:20 PM.

+ 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. Getting run time error while copy / paste range
    By rkayasth in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-07-2016, 08:56 AM
  2. [SOLVED] When looping through Workbooks calling Macro - Run-time error '9': Subscript out of range
    By trizzo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2015, 07:17 AM
  3. "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
  4. [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
  5. [SOLVED] Error 09 - Subscript out of Range - For Workbooks.sheets Object
    By Marceltcm in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-12-2013, 08:03 PM
  6. Comparing values in 2 workbooks - Subscript out of range error
    By workmohit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-03-2013, 09:58 AM
  7. Run-time error 9: subscript out of range when trying to close workbooks
    By jhigashi in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-27-2013, 09:04 PM

Tags for this Thread

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