Consolidation Macro - All Input files in folder

Vbalearner85

Board Regular
Joined
Jun 9, 2019
Messages
139
Office Version
  1. 2016
Platform
  1. Windows
Hi Vba champs,

1 Need help for macro which should consolidate n number of Input files(all saved in "Input" folder) in 1 summary file
2.Each Input file has same format/ 2 tabs - 1-Input Details 2. Summary. Need each tab in Input file to consolidate(copy as values/No format copy) separately in 2 existing tabs by same names in "Summary file". Tab 1 has data from row 4 in each Input file and Tab 2 has data from row 6 to be consolidated.
3.All Input files have same no. of columns(around 10)/same headers(so no need to copy headers.it should only copy data in rows.in summary file ...no. of rows will vary for each input file)
4.Macro should take auto path for all Input files from sub-folder "Input" where main "summary" file is saved

Thanks in Advance
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this

Code:
Option Explicit
Sub Consolidation_Macro()
    Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet, sh As Worksheet
    Dim wPath As String, wFiles As Variant, e1 As Boolean, e2 As Boolean
    Dim lr1 As Long, lr2 As Long
    
    Application.ScreenUpdating = False
    
    Set wb1 = ThisWorkbook
    Set sh1 = wb1.Sheets("Input Details")
    Set sh2 = wb1.Sheets("Summary")
    
    wPath = wb1.Path & "\Input\"
    
    wFiles = Dir(wPath & "*.xlsx")
    Do While wFiles <> ""
        Set wb2 = Workbooks.Open(wFiles)
        e1 = False
        e2 = False
        For Each sh In wb2.Sheets
            If LCase(sh.Name) = LCase(sh1.Name) Then e1 = True
            If LCase(sh.Name) = LCase(sh1.Name) Then e2 = True
        Next
        If e1 And e2 Then
            lr1 = sh1.Range("A" & Rows.Count).End(xlUp).Row + 1
            lr2 = wb2.Sheets(sh1.Name).Range("A" & Rows.Count).End(xlUp).Row
            wb2.Sheets(sh1.Name).Range("A4:J" & lr2).Copy
            sh1.Range("A" & lr1).PasteSpecial xlPasteValues
            
            lr1 = sh2.Range("A" & Rows.Count).End(xlUp).Row + 1
            lr2 = wb2.Sheets(sh2.Name).Range("A" & Rows.Count).End(xlUp).Row
            wb2.Sheets(sh2.Name).Range("A6:J" & lr2).Copy
            sh2.Range("A" & lr1).PasteSpecial xlPasteValues
        End If
        wb2.Close False
        wFiles = Dir()
    Loop
    Application.ScreenUpdating = True
    MsgBox "End"
End Sub
 
Upvote 0
Appreciate your effort....
Code is giving runtime error "1004" "Sorry..couldn't locate A.xlsx.. could be moved.renamed or deleted"..A.xlsx is first Input file. Not sure whats wrong..cross-checked that tab names match in consolidation and Input files at my end....
 
Upvote 0
Appreciate your effort....
Code is giving runtime error "1004" "Sorry..couldn't locate A.xlsx.. could be moved.renamed or deleted"..A.xlsx is first Input file. Not sure whats wrong..cross-checked that tab names match in consolidation and Input files at my end....


On which line does the macro stop?

Change this line

Code:
Set wb2 = Workbooks.Open(wFiles)

By

Code:
[COLOR=#333333]Set wb2 = Workbooks.Open([/COLOR][COLOR=#333333]wPath & [/COLOR][COLOR=#333333]wFiles)[/COLOR]
 
Upvote 0
Fantastic... Works like as charm now..as desired...Tested it with changing number of Input files..works perfectly!!!

Many Thanks!!.. for your valuable time :) :)
 
Upvote 0
Fantastic... Works like as charm now..as desired...Tested it with changing number of Input files..works perfectly!!!

Many Thanks!!.. for your valuable time :) :)

I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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