Closed Thread
Results 1 to 7 of 7

Macro to Merge different Excel files into one

Hybrid View

sukanyae Macro to Merge different... 02-02-2010, 11:20 PM
JBeaucaire Re: Macro to Merge different... 02-02-2010, 11:37 PM
sukanyae Re: Macro to Merge different... 02-03-2010, 03:12 AM
Abhilash B Re: Macro to Merge different... 07-06-2012, 10:07 AM
JBeaucaire Re: Macro to Merge different... 02-03-2010, 12:25 PM
premmi Re: Macro to Merge different... 06-12-2011, 05:12 PM
arlu1201 Re: Macro to Merge different... 07-06-2012, 10:27 AM
  1. #1
    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!)

  2. #2
    Registered User
    Join Date
    01-27-2010
    Location
    SFO, USA
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: Macro to Merge different Excel files into one

    Hi JBeucarie,

    Thanks so much for the code. It is working. But I want the sheet/tab names as actual sheet/tab names from all excel files. Currently It is taking file names as Tab names.

    Is that possible ?

    Thanks,
    /Sukanya

    Quote Originally Posted by JBeaucaire View Post
    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

  3. #3
    Registered User
    Join Date
    07-06-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    3

    Thumbs up Re: Macro to Merge different Excel files into one

    Hi Forum,

    Thanks so much for the code. It is working. Currently in the macro we have given a path to the files, so anyone using the macro has to create the same folders etc. I wanted to change it like once we run the macro it should ask for the path, like it should only point to dir and then we can select the path where the files are saved.

    Also I want to clear the color (unfill) on the final workbook.

    Is that possible ?

    Thanks,
    Abhilash
    Last edited by Abhilash B; 07-06-2012 at 10:29 AM.

Closed 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