VBA - Combine Multiple workbooks into 1 worksheet

30136353

Board Regular
Joined
Aug 14, 2019
Messages
55
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.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,512
Try:
VBA Code:
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim MyFolder As String, MyFile As String, wkbSource As Workbook, wsDest As Worksheet, ws As Worksheet, 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 Each ws In Sheets
            LastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            With wsDest
                ws.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
        Next ws
        MyFile = Dir
        wkbSource.Close False
    Loop
    Application.ScreenUpdating = True
End Sub
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,255
Office Version
2013
Platform
Windows
@mumps

Like solution & hope don't mind comment but would the line be safer with qualifying?

Rich (BB code):
For Each ws In wbSource.Worksheets
Dave
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,512
@dmt32
Since that line of code applies to the active workbook which in this case is the newly opened wkbSource, you really don't need to fully qualify. However, you could do so if you wanted.
 

30136353

Board Regular
Joined
Aug 14, 2019
Messages
55
Try:
VBA Code:
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim MyFolder As String, MyFile As String, wkbSource As Workbook, wsDest As Worksheet, ws As Worksheet, 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 Each ws In Sheets
            LastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            With wsDest
                ws.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
        Next ws
        MyFile = Dir
        wkbSource.Close False
    Loop
    Application.ScreenUpdating = True
End Sub
Hi, thanks for the help. When running the macro I got a run time error '91', to do with:

LastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,512
Do you have any blank sheets in any of the source workbooks?
 

30136353

Board Regular
Joined
Aug 14, 2019
Messages
55
No there shouldn't be, I just need sheets 1 - 5, Row 1 - 500 from every workbook... Cheers
 

Forum statistics

Threads
1,081,856
Messages
5,361,718
Members
400,650
Latest member
tcisrly

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top