Consolidate all values from specific sheets into 1 master excel file

yrga88

New Member
Joined
Oct 1, 2024
Messages
1
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have master excel file with some of sheets names are ("abc", "def", and "ghi"). In the same folder as master excel, there is 1 folder named 'Data' where inside it there are 20 excel workbooks which also has same sheet names ( "abc", "def", and "ghi"). I want to have vba codes inside the master workbook to copy paste and consolidate all values in each of these sheet names ("abc", "def", and "ghi") from 20 excel workbooks to master excel file.

I can provide with dummy data if that's needed.

Thank you!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi yrga88,

Welcome to MrExcel!!

See how this goes:

VBA Code:
Option Explicit
Sub Macro1()
    
    Dim strSrcPath As String, strFile As String
    Dim wb As Workbook
    Dim blnWasFileOpen As Boolean
    Dim wsSrc As Worksheet, wsDestin As Worksheet
    Dim varSheets As Variant
    Dim i As Long
    Dim strCopyRange As String
    
    strSrcPath = "C:\Users\yrga88\Desktop" 'Directory path containing Excel files. Change to suit.
    strSrcPath = IIf(Right(strSrcPath, 1) <> "\", strSrcPath & "\", strSrcPath)
    strFile = Dir(strSrcPath & "*.xls*") 'Consolidate Excel files only
    varSheets = Array("abc", "def", "ghi") 'Sheet names to be consolidated. Change to suit.
    
    Application.ScreenUpdating = False
    
    If Dir(strSrcPath, vbDirectory) = "" Then
        MsgBox "The path """ & strSrcPath & """ is invalid." & vbNewLine & "Please check and try again.", vbExclamation
        Exit Sub
    End If
    
    Do While Len(strFile) > 0
        On Error Resume Next
            Set wb = Workbooks(CStr(strFile))
            If Err.Number <> 0 Then
                Set wb = Workbooks.Open(strSrcPath & strFile, UpdateLinks:=False, ReadOnly:=True)
            Else
               blnWasFileOpen = True
            End If
        On Error GoTo 0
        For Each wsSrc In wb.Worksheets
            If IsNumeric(Application.Match(wsSrc.Name, varSheets, 0)) Then
                Call UnhideShowAll(wsSrc)
                Set wsDestin = ThisWorkbook.Sheets(CStr(wsSrc.Name))
                Call UnhideShowAll(wsDestin)
                strCopyRange = Split(wsSrc.UsedRange.Offset(1, 0).Address, ":")(0) 'Assumes Row 1 is for headings. Change offset number to suit.
                strCopyRange = strCopyRange & ":" & Split(wsSrc.UsedRange.Address, ":")(1)
                On Error Resume Next
                    i = wsDestin.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                    i = IIf(i = 0, 2, i)
                On Error GoTo 0
                wsSrc.Range(strCopyRange).Copy Destination:=wsDestin.Range("A" & i)
            End If
        Next wsSrc
        If blnWasFileOpen = False Then
            Application.DisplayAlerts = False
                wb.Close SaveChanges:=False
            Application.DisplayAlerts = True
        End If
        blnWasFileOpen = False
        strFile = Dir
    Loop
    
    Application.ScreenUpdating = True
    
    MsgBox "Files have now been consolidated.", vbInformation

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,224,259
Messages
6,177,485
Members
452,782
Latest member
ZCapitao

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