+ Reply to Thread
Results 1 to 17 of 17

Open all xls Files in folder & Copy To 1 Spreadsheet

  1. #1
    Registered User
    Join Date
    06-08-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    68

    Open all xls Files in folder & Copy To 1 Spreadsheet

    I’m wondering if this is possible, if I have a folder with say 30 excel spreadsheets (.xls) all named differently (number of files will always change), can I easily write something in VBA to Open all the spreadsheets and copy each sheet over to an existing Excel spreadsheet? For example, have a ‘template’ spreadsheet where the VBA would exist, then have the first sheet, (Sheet1) of each 30 sheets be copied back over to the template.xls? Even better, could I rename each Sheet1 to the name of the file before copying it over? This would basically be the first step in my process of getting the spreadsheet made.


    I found this code in a similar question, so how can I rename the Sheet to the opened file name, then copy that over to template.xls?

    Please Login or Register  to view this content.
    Last edited by Tyler_Durden; 11-12-2009 at 12:56 PM.

  2. #2
    Registered User
    Join Date
    11-11-2009
    Location
    Alpharetta, GA
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    I use this macro to combine my xls files into one spreadsheet. Run this macro in a new workbook in the same folder as your files that you want to copy. Then just run it. It will open and copy all of the files and combine them into this new workbook. And you do not have to keep renaming your folder location. You just add this code to a blank workbook and you can save it as a template, by just renaming it each time. Or just delete the old data and run it again. You do not have to rename or change this code ever. Works great!


    Please Login or Register  to view this content.

    If you use Excel 2007 then just replace the references "A1:IV" and "A65536" with "A1:XFD" and "A1048576:.
    Last edited by mslynng; 11-12-2009 at 09:38 AM.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Hi,

    This is another option. It copies Sheet1 from every workbook opened. If there are several sheets and you need it to copy each one then you'll need another For..Next loop inside the one shown to cycle through all sheets and copy.

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    06-08-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    68

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    I must be missing something, when I run this it seems to work, opening each file in the directory and renaming each sheet1 to the file name, however when it stops running, there are no sheets opened or moved. I took out the line wbTemp.Close and that seemed to keep all the sheets open, however none were moved over to my ‘Template’ spreadsheet. Isn’t this supposed to move the sheets behind wbMain? Should I redefine wbMain something like Template.xls?

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-08-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    68

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Strange I just reran it, and it seems to work, thanks for all of your help!

  6. #6
    Registered User
    Join Date
    06-08-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    68

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Sorry to come back to this after I already put solved on it, but is there a line I can add that will not add the extension to each sheet? It works to rename the sheets the opened workbooks file name, but can I have it not add '.xls'?

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Hi,

    Instead of
    Please Login or Register  to view this content.
    use
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-08-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    68

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Works perfectly, again thank you

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Hi,

    I realise that this will be OK provided there are no sheet names longer than 31 characters. It looks like Excel won't allow file names longer than this, hence the original truncation at 31 characters.

    This second instruction could theoretically result in more than 31 characters. Hence it would be best if after this instruction you added a second.

    Please Login or Register  to view this content.
    Rgds

  10. #10
    Registered User
    Join Date
    06-08-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    68

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Ok, great thank you. I think for this that won’t be an issue, but I will keep this in mind for the future..

    I’m wondering if in the original code you gave me, I could get some help on adding this in..

    Could I look for a value in column A, If ‘Taxable Fixed Income’ then copy just that row, else copy ‘Account’ row, to sheet 2, then instead of renaming sheet 1, we could rename sheet 2 and copy that over to the main workbook?

    Again thanks for all of your help I couldn’t have gotten this far without it.

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Hi,

    First a point of clarification.

    The existing code doesn't change anything in the file that's being copied. It merely opens it, temporarily changes the sheet name so that when the whole sheet is copied to the main workbook the sheet name is the name of the opened file. The 'daughter' file is then closed without saving, thus the original sheet name is retained. You make mention of changing sheet2. Does that imply that you want to permanently change the sheet2 name in the daughter file, or just the name of the sheet to which it's copied in the main workbook.

    Then can you clarify this latest request.

    Are you saying that you still want to copy over a complete sheet, but if the condition you mention is true, you want to update sheet 2 of the daughter file first, and then copy sheet 2 to the main. And is it JUST sheet 2 in this event or sheet 1 and 2?

    On the condition.

    Is there only ever one row occurrence of both 'Taxable Fixed Income' and 'Account' or could there be several or none?

    If none then in that case do you still want to copy sheet1?

    Finally where on sheet 2 do you want to copy the Taxable Fixed Income/Account row(s)

    Rgds

  12. #12
    Registered User
    Join Date
    06-08-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    68

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Sorry for the ramble…

    You are correct, the current code only takes the opened file, renames it and copies it. That is working great.

    The ideal objective of this is to get just the row if there is taxable fixed income or account.. there can only be one taxable fixed income row, however there can be a taxable fixed income row and an account row. In that situation, I would want the taxable fixed income. Some sheets do not have taxable fixed income, in that situation I want just Account. I was saying that if its easier to copy either row of that condition to the second sheet of the temp file, rename that sheet and copy that sheet over. It can just be in Column A Row 1. Thanks for your patience on this.

  13. #13
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Hi,

    If I've understood correctly the following will copy either the TFI or Account row of sheet1 to A1 on sheet2 and then copy sheet 2 to the main workbook as before.

    Please Login or Register  to view this content.
    HTH

  14. #14
    Registered User
    Join Date
    06-08-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    68

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Works perfectly, thank you so much.

  15. #15
    Registered User
    Join Date
    06-11-2010
    Location
    Manila
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Quote Originally Posted by mslynng View Post
    I use this macro to combine my xls files into one spreadsheet. Run this macro in a new workbook in the same folder as your files that you want to copy. Then just run it. It will open and copy all of the files and combine them into this new workbook. And you do not have to keep renaming your folder location. You just add this code to a blank workbook and you can save it as a template, by just renaming it each time. Or just delete the old data and run it again. You do not have to rename or change this code ever. Works great!


    Please Login or Register  to view this content.

    If you use Excel 2007 then just replace the references "A1:IV" and "A65536" with "A1:XFD" and "A1048576:.
    Hi mslynng,
    I tried this code but it only copies until six sheets/tabs. I believe this code will copy all the xls file inside the folder where you put this macro file. Am I correct?
    Thanks!

    *note: no idea how I shall post my question here. Should I create a new thread for this? I know I should send a private msg to mslynng.. So please correct me if I am wrong

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.

    So yes, please start your own thread!
    Remember what the dormouse said
    Feed your head

  17. #17
    Registered User
    Join Date
    06-11-2010
    Location
    Manila
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: Open all xls Files in folder & Copy To 1 Spreadsheet

    okidoks! noted!

    Quote Originally Posted by romperstomper View Post
    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.

    So yes, please start your own thread!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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