Results 1 to 9 of 9

Thread: Code to move data from one spreadsheet to another

  1. #1
    New Member
    Join Date
    Oct 2016
    Posts
    20
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post Code to move data from one spreadsheet to another

    Hi, I'm trying to move data from one spreadsheet to another (instead of using a copy and paste code which uses too much memory). I'm trying to reduce a 70MB file down to something more manageable and think removing the copy and paste function will help a lot.

    In workbook "Book1" I'm trying to move data from sheetx to sheety in workbook "Book2" however I'm getting the nasty debug error (runtime error 9)

    Windows("Book1.xlsx").Activate
    Sheets("Sheetx").Range("B10:AX5000").Copy Destination:=Windows("Book2.xlsx").Activate.Sheets("Sheety").Range("A2:AX5000")


    Any ideas? Thanks in advance

    Woof

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,190
    Post Thanks / Like
    Mentioned
    470 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Code to move data from one spreadsheet to another

    Try
    Code:
    Sheets("Sheetx").Range("B10:AX5000").Copy Destination:=workbooks("Book2.xlsx").Sheets("Sheety").Range("A2")
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    New Member
    Join Date
    Oct 2016
    Posts
    20
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Code to move data from one spreadsheet to another

    Thanks for this, but I'm still getting the run time error. I'm assuming that it could be that the macro is saved in "Book2" rather than "Book1", apologies for not mentioning that. I need to pull the data into book2 rather than push it from book1 (as I will be replicating the code several times for several different sheets). Will that make a difference to the code?

  4. #4
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,190
    Post Thanks / Like
    Mentioned
    470 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Code to move data from one spreadsheet to another

    What is the error message that you get?
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  5. #5
    New Member
    Join Date
    Oct 2016
    Posts
    20
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Code to move data from one spreadsheet to another

    I get a run time error 9 "subscript out of range"

  6. #6
    Board Regular
    Join Date
    Dec 2017
    Location
    UK
    Posts
    904
    Post Thanks / Like
    Mentioned
    37 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Code to move data from one spreadsheet to another

    Windows("Book1.xlsx").Activate
    Sheets("Sheetx").Range("B10:AX5000").Copy Destination:=Windows("Book2.xlsx").Activate.Sheets("Sheety").Range("A2:AX5000")
    If your macro is in Book2, then the extension cannot be xlsx since this type of file prohibits macros. I would presume that it is xlsm. I suggest changing the extension and seeing if that solves your problem
    Speed up your code use variant arrays and NEVER ACCESS THE WORKSHEET IN A LOOP

  7. #7
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,190
    Post Thanks / Like
    Mentioned
    470 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Code to move data from one spreadsheet to another

    That error signifies that one of the workbook names (including extension) or sheet names is wrong.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  8. #8
    New Member
    Join Date
    Oct 2016
    Posts
    20
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Code to move data from one spreadsheet to another

    Perfect, it works. I'll replicate it for the other tabs and hopefully it will reduce the memory size of the file. Thanks very much for your help

  9. #9
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,190
    Post Thanks / Like
    Mentioned
    470 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Code to move data from one spreadsheet to another

    You're welcome & thanks for the feedback
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •