+ Reply to Thread
Results 1 to 20 of 20

Copy ranges from different worksheets

  1. #1
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Copy ranges from different worksheets

    Hi Guys,

    I need help to copy a range in multiple worksheets in different workbooks and paste them into different worksheets in the active workbook.
    I have code that will copy a range in a worksheet and paste it into another worksheet in a different workbook, this all works well but I really need some help on how I could modify it to copy multiple worksheets.

    Hope someone can help me?

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    You need a looping structure.
    The loop can open each of multiple predetermined workbooks,
    Then loop through multiple sheets in that current open workbook and copy the data to the destination workbook.

    In order to assist we need more info about where the multiple source workbooks reside (in a folder)
    What sheets contain the info you need copied and if it can be the entire sheet or just a specific range, and if a range, is it the same range each time, or could it be different sizes of ranges.


    IT would be great if you can upload a sample file that illustrates the source data to copy. Please ensure it is desensitized.
    Regards,
    Rudi

  3. #3
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Hi Rudi,

    Thank you so much for replying!

    The multiple Source books will reside in the following folder C:\Users\HQ\Downloads and the Destination Workbook will reside C:\Users\HQ\Documents\Data.
    Each source Workbook contains just 1 Worksheet and the ranges differ from sheet to sheet but you could just copy the entire column or row depending what info is on that worksheet. The Destination workbook would normally be open and the Source workbooks closed. In the Destination workbook there will be worksheets corresponding to the worksheets to be copied in the Source workbooks.
    I have attached 3 Sample Source workbooks and the Destination workbook

    Sample Source Workbooks
    sales-by-hour-42233.csv
    sales-by-month-71888.csv
    sales-by-period-78904.csv

    Destination Workbook
    Final Destination.xlsm

    Hope the above helps?
    Regards

    Derek
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    Thanks for the concise and clear reply.
    I'll see that I can set up for you...

  5. #5
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    Hi,

    Give this Sales Import Template a run and see it it does what you need...
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Hi Rudi,

    I tested out and looks very impressive but there is a couple of things I didn't explain properly to you. So Sorry!

    I don't need to remove any of the worksheets in the Destination folder because I have other data and formulas on all the worksheets in the Destination workbook that I need to keep.
    I need to copy the exact Columns or Rows "Respectively" on the worksheet in the respective Source workbook and then paste it into the respective worksheet in the Destination workbook with the same Range eg: Col "A:Z" or Row "1:8" etc.

    So for example the Source Workbook "sales-by-hour-42233.csv" has a worksheet called "sales-by-hour-42233". I need to copy the column range "A:Z" and then paste it into a worksheet in the Final Destination.xlsm workbook called "sales by hour" who's range would be "A:Z" the same as the source worksheet.

    were as ................

    for example the Source Workbook "sales-by-month-71888.csv" has a worksheet called "sales-by-month-71888.csv". I need to copy the Row range "1:8" and then paste it into a worksheet in the Final Destination.xlsm workbook called "sales by month" who's range would be "1:8" the same as the source worksheet.


    Instead of picking a Source workbook every time is it possible to copy all of the Source workbooks respectively and paste them into their respective worksheets in the Destination Workbook using their respective Source worksheet ranges? All of the Source workbooks are always going to be in the Download folder so I would be picking them all!

    Hope I haven't confused you more

    regards

    Derek

  7. #7
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    Hi,

    Here is the updated macro.
    I have scrapped the button meaning that you will need to run the macro from the VIEW-MACROS ribbon button or by pressing ALT+F8 and then run the macro called: GetSalesCSVData

    If you need the macro to run in another file, just copy it from this workbook to that file...

    Run it and see if it is doing what you need...
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Rudi..... U dah man!..... exactly what I wanted to the T!

    Thank you so so much, this will save me allot of time coping & pasting etc.

    Can I be brazen and ask if you could explain the logic to the code? I can figure out some of it but other parts are beyond me!

    Cheers....

  9. #9
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    Hi,

    Good to hear it is working, and glad I could help.

    Regarding the code...

    This sets the base location and stores it in the Folder variable.
    sFile is another variable that stores the first file in the sFolder location
    Dir() is a VB function that collects files in a folder (in this case specifically CSV files based on the *.csv extention)
    Please Login or Register  to view this content.
    ScreenUpdating switches off the screen refresh...speeding up the macro and preventing screen movement
    The Do loop makes use of the VB function InStr() to inspect the file name loaded into the sFile variable by Dir.
    The syntax is: InStr(Phrase to search, Term to search for within the Phrase)
    The InStr(...)>1 (ideally could be >0; I changed it below, though it will not make a difference in your macro) means that if it finds the term in the phrase, the returning value of InStr = the start position of the term. Basically, if it finds the term in the phrase it will be >0 (eg: =InStr("Hello big world", "big") will equal 7)
    It determines if the filename has any of three key words (hour, period and month). It needs this to know which sheet it is processing and to which destination sheet it needs to paste the data. Having said this...it is important that these terms are present in the file name and the sheet names.
    Once the
    Please Login or Register  to view this content.
    Here the code opens the referenced workbook from the file path
    Stores the file name of the opened workbook
    Copies the current region range from the source to cell A1 in the destination sheet
    Closes the source file
    CurrentRegion is a special selection property that extends a selected cell outwards until it encompasses its entire block.
    Lastly, Dir() is called again to populate the sFile variable with the next File name in the folder.
    The Do...Loop will execute until the last file in the folder (that is CSV in this case...based on the filter *.csv)
    Please Login or Register  to view this content.
    This finished the macro by switching screen refresh mode back on.
    Please Login or Register  to view this content.
    Hope that explains a bit more about the code...
    Last edited by RudiS; 05-03-2014 at 03:51 PM.

  10. #10
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Hi Rudi,

    Sorry for the late reply!
    Thanks for explaining the code to me, makes allot more sense to me now!

    Again thank you for all your help!

    regards

    Derek

  11. #11
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Hi Rudi,

    So Sorry to bother you again but I forgot to ask is it possible to not only to look for ".csv" files in the code but also other file types like ".xls" & "xlsm"???

    regards

    Derek

  12. #12
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    Hi,

    The Dir() function can only take 1 extension argument. If you need to process CSV's as well as XLS* combinations you will need to use:
    sFile = Dir(sFolder & "\*.*") - which will process any and all files in the folder and test each one for the extension before further processing it.

    Of course, if it is ONLY excel files, you can use:

    sFile = Dir(sFolder & "\*.xls*")

    Let me know what is required....

  13. #13
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Hi Rudi,

    It would be mostly be .csv & .xls files

    regards

    D

  14. #14
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    Hi Derek,

    Replace your current code with this new code (below). It checks for the two file types to process.

    Please note that the *.xls type files must have the data on sheet 1 of each of these files. A CSV file usually only has one sheet so I have made no reservations in the code to check for sheet counts, etc. If the data you plan to transfer is not on sheet 1 (the first sheet) in the *.xls files, the data will not come through to the destination properly.

    Cheers!

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Hi Rudi,

    I tested the code but it comes up with an error "Subscript out of range" the following line in the code is highlighted

    "ActiveSheet.Range("A1").CurrentRegion.Copy ThisWorkbook.Sheets(sName).Range("A1")"

    I added two extra sheets onto the code but I think the problem lies with the "Expenses by Contact.xls" Workbook & worksheet? On this sheet the 1st row is Merged
    from cells A1 to F1 and the three subsequent rows after that as well. I think this is why we are getting the error??

    I have attached the files so you know what I mean.

    D
    Attached Files Attached Files

  16. #16
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    Hi,

    This code will work...
    The problem was that the Expenses sheets data did not start in Cell A1 (as the csv files usually do).
    I compensated for this by changing the range property called CurrentRegion with a Sheet property called UsedRange, which has a wider scope in getting the block of data to copy.

    Test if this code runs OK...

    Please Login or Register  to view this content.
    Last edited by RudiS; 05-04-2014 at 03:37 PM.

  17. #17
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Hi Rudi,

    I tested it, but even though it says "importing Sheets Complete" nothings importing at all?

    Strange??

    D

  18. #18
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    Oops...sorry my fault.
    I forgot to remove my path for testing.

    I edited the macro above and removed my test path and replace it with your path again: sFolder = "C:\Users\HQ\Downloads"

    If you copy the macro above again and run...it should work fine now!

  19. #19
    Registered User
    Join Date
    06-20-2012
    Location
    Limerick Ireland
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Copy ranges from different worksheets

    Hi Rudi,

    I found the problem the sFolder = "C:\Users\rudis\Downloads" ..... should be ....... sFolder = "C:\Users\HQ\Downloads"

    Where it says "rudis" it should be "HQ" for me. Other than that it worked like a charm!

    Again what can I say? U Dah Man! Thanks so much for going beyond the call of duty to get this right for me!

    regards

    D

  20. #20
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Copy ranges from different worksheets

    It was a pleasure to help...
    See you around...

+ 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. Copy ranges from multiple worksheets in excel and then paste special in Powerpoint
    By mclarke2030 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-25-2013, 04:47 PM
  2. Replies: 2
    Last Post: 07-17-2012, 09:18 PM
  3. Copy ranges between different worksheets
    By a.mack123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2010, 07:55 PM
  4. [SOLVED] Open a template, copy 3 worksheets w ranges and save as workbook
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-03-2006, 01:35 AM
  5. copy ranges from multiple worksheets
    By simora in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-17-2005, 09:06 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