merge data from different workbooks into a master workbook - WITH a TWIST!

hrithik

Active Member
Joined
Jul 26, 2010
Messages
336
I'm using this macro to merge all xls files in a folder to a single sheet master file.

I want to append the Filename in the end column in the Master File so that I exactly know which file that data came from.

Experts please help!

Code:
'Description: Combines all files 1st sheet in a folder to a master file
Sub MergeFilesWithoutSpaces()
    Dim path As String, ThisWB As String, lngFilecounter As Long
    Dim wbDest As Workbook, shtDest As Worksheet, ws As Worksheet
    Dim Filename As String, Wkb As Workbook
    Dim CopyRng As Range, Dest As Range
    Dim RowofCopySheet As Integer

    ThisWB = ActiveWorkbook.Name
    
    path = GetDirectory("Select a folder containing Excel files you want to merge")
    
    RowofCopySheet = InputBox("Enter Row to start copy on") ' Row to start on in the sheets you are copying from

    Application.EnableEvents = False
    Application.ScreenUpdating = False

    Set shtDest = ActiveWorkbook.Sheets(1)
    Filename = Dir(path & "\*.xls", vbNormal)
    If Len(Filename) = 0 Then Exit Sub
    Do Until Filename = vbNullString
        If Not Filename = ThisWB Then
            Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
            Set CopyRng = Wkb.Sheets(1).Range(Cells(RowofCopySheet, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, Cells(1, Columns.Count).End(xlToLeft).Column))
            Set Dest = shtDest.Range("A" & shtDest.Cells(Rows.Count, 1).End(xlUp).Row + 1)
            CopyRng.Copy
            Dest.PasteSpecial xlPasteValuesAndNumberFormats
            Application.CutCopyMode = False 'Clear Clipboard
            Wkb.Close False
        End If
        
        Filename = Dir()
    Loop

    Range("A1").Select
    Columns.AutoFit
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    MsgBox "Done!"
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I can't really test this since I'd have to build some dummy files and such, but it looks like you've don ethe hard part.

You just need to define the last column in your copy file, the last row in your destination file, and then set the vlaue in the column after the last column in the row after your last column and then filldown.


Put this right after the "Set Dest =" line
Code:
LastRow1 = shtDest.Cells(Rows.Count, 1).End(xlUp).Row 
LastCol = Wkb.Sheets(1).Cells(RowofCopySheet, columns.count).End(xltoLeft).column

Put this after your pastespecial
Code:
shtDest.Cells(lastrow+1,lastcol+1) = filename
LastRow2 = shtDest.Cells(Rows.Count, 1).End(xlUp).Row 
shtDest.Range(Cells(lastrow1+1,lastcol+1),Cells(lastrow2,lastcol+1)).FillDown

You might have to play with it, but I think you already have everything you need. It might be even easier than what i came up with, your code is pretty sophisticated.
 
Upvote 0
Thanks for your reply, Ryan

I'm not able to make it work with the changes you suggested.
Not sure which part is causing the problem, could be the range definitions
 
Upvote 0
What error are you getting? Have you tabbed (F8) through to see if the ranges are defining properly, and if not, whcih ones.
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,486
Members
452,917
Latest member
MrsMSalt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top