Results 1 to 7 of 7

Macro to Merge different Excel files into one

Threaded View

  1. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to Merge different Excel files into one

    I have a standard Consolidation macro to do this. Edit the lines marked in red to your needs and it should do what you want.

    Option Explicit
    
    Sub Consolidate()
    'Open all Excel files in a specific folder and import data as separate sheets
    'Renames imported sheets to match workbook name of each source file
    'JBeaucaire (7/6/2009)     (2007 compatible)
    Dim strFileName As String, strPath As String
    Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    
    Set wbkNew = ThisWorkbook
    strPath = "C:\Documents and Settings\Jerry\Files\"    'Edit to your path, be sure to end with \
    If Left(strPath, 1) <> "\" Then strPath = strPath & "\"
    
    strFileName = Dir(strPath & "*.xl*")
    wbkNew.Activate
    
    'Clear existing files (optional, remove this section if appending is desired)
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Temp"
        For Each ws In Worksheets
            If ws.Name <> "Temp" Then ws.Delete
        Next ws
        
    'Import first active sheet from found file
        Do While Len(strFileName) > 0
            Set wbkOld = Workbooks.Open(strPath & strFileName)
            ActiveSheet.Name = Left(strFileName, Len(strFileName) - 4)
            ActiveSheet.Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)
            strFileName = Dir
            wbkOld.Close False
        Loop
        
    wbkNew.Sheets("Temp").Delete
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub
    Last edited by JBeaucaire; 02-03-2010 at 12:27 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

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