VBA - Combine Multiple workbooks into 1 worksheet

30136353

Board Regular
Joined
Aug 14, 2019
Messages
105
Hi,

I am trying to write a VBA script which will combine multiple workbooks into 1 worksheet, copy and pasting all columns as values. Each sheet from the multiple workbooks should be pasted below the last, and ideally there would be the workbook name pasted into column A of the master as a unique identifier. The multiple workbooks would be stored in a folder on my desktop, but id like the macro to pop up and allow me to browse and select the folder.... I have seen a lot online but not quite what I'm after, any help? Thanks.
 
Merged cells almost always cause problems for macros and should be avoided at all cost. The merged cells would have to be unmerged. That can be done manually or the macro can do it for you. Please advise.
The macro will be able to do it as the sheets which are merged are not of value anyway... Thanks
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try:
VBA Code:
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim MyFolder As String, MyFile As String, wkbSource As Workbook, wsDest As Worksheet, x As Long, LastRow As Long
    Set wsDest = ThisWorkbook.Sheets("Sheet1")
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder"
        .Show
        .AllowMultiSelect = False
        If .SelectedItems.Count = 0 Then
            MsgBox "You did not select a folder."
            Exit Sub
        End If
        MyFolder = .SelectedItems(1) & "\"
    End With
    MyFile = Dir(MyFolder)
    Do While MyFile <> ""
        Set wkbSource = Workbooks.Open(Filename:=MyFolder & "\" & MyFile)
        For x = 1 To 3
            If WorksheetFunction.CountA(Sheets(x).Cells) <> 0 Then
                With Sheets(x)
                    .Cells.UnMerge
                    LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                End With
                With wsDest
                    Sheets(x).UsedRange.Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0)
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(LastRow) = wkbSource.Name
                End With
            End If
        Next x
        MyFile = Dir
        wkbSource.Close False
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim MyFolder As String, MyFile As String, wkbSource As Workbook, wsDest As Worksheet, x As Long, LastRow As Long
    Set wsDest = ThisWorkbook.Sheets("Sheet1")
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder"
        .Show
        .AllowMultiSelect = False
        If .SelectedItems.Count = 0 Then
            MsgBox "You did not select a folder."
            Exit Sub
        End If
        MyFolder = .SelectedItems(1) & "\"
    End With
    MyFile = Dir(MyFolder)
    Do While MyFile <> ""
        Set wkbSource = Workbooks.Open(Filename:=MyFolder & "\" & MyFile)
        For x = 1 To 3
            If WorksheetFunction.CountA(Sheets(x).Cells) <> 0 Then
                With Sheets(x)
                    .Cells.UnMerge
                    LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                End With
                With wsDest
                    Sheets(x).UsedRange.Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0)
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(LastRow) = wkbSource.Name
                End With
            End If
        Next x
        MyFile = Dir
        wkbSource.Close False
    Loop
    Application.ScreenUpdating = True
End Sub
Still the same error unfortunately
 
Upvote 0
Unless you can upload a copy of the problem workbook, I'm afraid I won't be able to help any further. :(
 
Upvote 0
We still have a few problems. In the second workbook the "Address" sheet is protected. Secondly, you want 2 sheets from one workbook and only one sheet from the other. Unless the number of sheets is the same for all workbooks, that complicates matters because you would have to call the sheets by their names. This means that you would need a list of the actual workbook names and the names of the sheets you want in each workbook.
 
Upvote 0
We still have a few problems. In the second workbook the "Address" sheet is protected. Secondly, you want 2 sheets from one workbook and only one sheet from the other. Unless the number of sheets is the same for all workbooks, that complicates matters because you would have to call the sheets by their names. This means that you would need a list of the actual workbook names and the names of the sheets you want in each workbook.
I am happy for the first 3 sheets to be taken from all workbooks, as it wont matter if i have excess data, i will be using formulas to look into the new master sheet. Can the macro unlock the workbooks before processing?
 
Upvote 0
I would need to know the password. Hopefully, the password is the same for all the sheets. If it's different, I'm afraid I will not be able to help.
 
Upvote 0
I would need to know the password. Hopefully, the password is the same for all the sheets. If it's different, I'm afraid I will not be able to help.
Mumps, thanks for all your help. I've actually just decided to use the merge for only the sheets without passwords, ive managed to change a few formulas to get all the information I was after. Regarding this piece of code, is there a way to tell it if no sheet 2 exists, just to skip it and not give an error?

Thanks:


VBA Code:
Do While MyFile <> ""
        Set wkbSource = Workbooks.Open(Filename:=MyFolder & "\" & MyFile)
        For x = 1 To 2
            If WorksheetFunction.CountA(Sheets(x).Cells) <> 0 Then
                With Sheets(x)
                    .Cells.UnMerge
                    LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                End With
                With wsDest
                    Sheets(x).UsedRange.Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(0, 0)
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(LastRow) = wkbSource.Name
                End With
            End If
        Next x
        MyFile = Dir
        wkbSource.Close False
    Loop
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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