+ Reply to Thread
Results 1 to 4 of 4

Combine multiple files into one workbook as separate tabs named from filenames

  1. #1
    Registered User
    Join Date
    04-11-2023
    Location
    Minneapolis
    MS-Off Ver
    2019
    Posts
    3

    Combine multiple files into one workbook as separate tabs named from filenames

    I have a folder with 25 files, one sheet each, all headers are the same. I want to combine all these .xlsx files into one workbook as one tab each using the filename from each file (not the tab names).

    The code below works to combine all the files, but I need to insert code which will rename the tabs based on filenames. This is important for creating unions out of Tableau.


    Sub mergeFiles()
    'Merges all files in a folder to a main file.

    'Define variables:
    Dim numberOfFilesChosen, i As Integer
    Dim tempFileDialog As FileDialog
    Dim mainWorkbook, sourceWorkbook As Workbook
    Dim tempWorkSheet As Worksheet

    Set mainWorkbook = Application.ActiveWorkbook
    Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)

    'Allow the user to select multiple workbooks
    tempFileDialog.AllowMultiSelect = True

    numberOfFilesChosen = tempFileDialog.Show

    'Loop through all selected workbooks
    For i = 1 To tempFileDialog.SelectedItems.Count

    'Open each workbook
    Workbooks.Open tempFileDialog.SelectedItems(i)

    Set sourceWorkbook = ActiveWorkbook

    'Copy each worksheet to the end of the main workbook
    For Each tempWorkSheet In sourceWorkbook.Worksheets
    tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
    Next tempWorkSheet

    'Close the source workbook
    sourceWorkbook.Close
    Next i

    End Sub

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,801

    Re: Combine multiple files into one workbook as separate tabs named from filenames

    After this line:

    Please Login or Register  to view this content.
    add

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    04-11-2023
    Location
    Minneapolis
    MS-Off Ver
    2019
    Posts
    3

    Re: Combine multiple files into one workbook as separate tabs named from filenames

    Works great, thanks!

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,801

    Re: Combine multiple files into one workbook as separate tabs named from filenames

    Just one nitpick that might be a useful lesson for you: Sheets and Worksheets are not the same thing - Graph sheets are part of the Sheets set but not the Worksheet set, so really you should change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.

+ 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. VBA combine multiple Excel files with separate worksheets
    By revocats10 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-11-2023, 06:32 PM
  2. Combine multiple worksheets (tabs) within one workbook
    By tufa4311 in forum Excel General
    Replies: 1
    Last Post: 02-03-2018, 09:35 PM
  3. [SOLVED] New to Excel programming. In need of a macro to combine multiple files with multiple tabs.
    By nflores0303 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-12-2016, 11:03 AM
  4. Moving multiple excel tabs into separate tabs/separate files
    By RO24 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-02-2014, 11:04 AM
  5. Macro to combine same-named sheets from multiple files
    By virsilens in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-13-2014, 12:05 AM
  6. Combine multiple files in one, under separate worksheets
    By plans in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2014, 04:04 AM
  7. Select sheet tabs in workbook & save to separate workbook files
    By stratocaster in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-01-2006, 11:40 AM

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