VBA code to combine files

dinesh shah

New Member
Joined
Aug 16, 2012
Messages
26
Dear friends,
I have .xlsm files a,b,c and d in a folder.
In sheet 1 of each file, range a4 to i75 I have data. I want to copy this data from each file to a master file say x.xlsm one below the other. Please help me with VBA code to do the same. Thanks in advance for any kind of help.
 
vba
Rich (BB code):
Sub test()
    Dim myDir$, e, wsName$, s$, c As Range, n&
    Const r$ = "A4:I75"
    Set c = Range(r): n = c.Row   '<--- change to any number you want for first row of output.
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then myDir = .SelectedItems(1) & "\"
    End With
    If myDir = "" Then Exit Sub
    Application.ScreenUpdating = False
    For Each e In Array("a.xlsm", "b.xlsm", "c.xlsm", "d.xlsm")
        If Dir(myDir & e) = e Then
            wsName = GetSheetName(myDir & e)
            s = "'" & myDir & "[" & e & "]" & wsName & "'!" & c(1).Address(0, 0)
            With ThisWorkbook.Sheets(1).Cells(n, 1).Resize(c.Rows.Count, c.Columns.Count)
                .Formula = Replace("=if(#<>"""",#,"""")", "#", s)
                .Columns(.Columns.Count + 1) = e
                .Value = .Value: n = n + .Rows.Count
            End With
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Function GetSheetName(fn As String) As String
    With CreateObject("DAO.DBEngine.120").workspaces(0).OpenDatabase(fn, True, True, "excel 12.0;HDR=No;")
        GetSheetName = Replace(.tabledefs(0).Name, "'", "")
        GetSheetName = Left$(GetSheetName, Len(GetSheetName) - 1)
        .Close
    End With
End Function
 
Upvote 0
vba
Rich (BB code):
Sub test()
    Dim myDir$, e, wsName$, s$, c As Range, n&
    Const r$ = "A4:I75"
    Set c = Range(r): n = c.Row   '<--- change to any number you want for first row of output.
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then myDir = .SelectedItems(1) & "\"
    End With
    If myDir = "" Then Exit Sub
    Application.ScreenUpdating = False
    For Each e In Array("a.xlsm", "b.xlsm", "c.xlsm", "d.xlsm")
        If Dir(myDir & e) = e Then
            wsName = GetSheetName(myDir & e)
            s = "'" & myDir & "[" & e & "]" & wsName & "'!" & c(1).Address(0, 0)
            With ThisWorkbook.Sheets(1).Cells(n, 1).Resize(c.Rows.Count, c.Columns.Count)
                .Formula = Replace("=if(#<>"""",#,"""")", "#", s)
                .Columns(.Columns.Count + 1) = e
                .Value = .Value: n = n + .Rows.Count
            End With
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Function GetSheetName(fn As String) As String
    With CreateObject("DAO.DBEngine.120").workspaces(0).OpenDatabase(fn, True, True, "excel 12.0;HDR=No;")
        GetSheetName = Replace(.tabledefs(0).Name, "'", "")
        GetSheetName = Left$(GetSheetName, Len(GetSheetName) - 1)
        .Close
    End With
End Function
 
Upvote 0

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