Merge files from a summary list and simultaneously add header info to the merged sheets

allenayres83

New Member
Joined
Jan 19, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I have workbook setup to use as a fieldbook for construction projects. The fieldbook will be organized by work item numbers (each work item will be a seperate worksheet). I have a folder setup that has a template for each work item and a summary in the main workbook that contains a list of work items for the project I am working on. I have a macro setup that will merge each work item that is in my summary from my templates folder to the main workbook. I have all of that working properly, but I need the macro to fill in some header information ,which can be found in the same row as the work item number, before it merges the next file. For example, Data!C11 has my item number. My macro will find that template (Lets say it is item number 201-01-00100) and merge it in to my workbook. I then want the macro to copy from Data!B11 to 201-01-00100!D3 and DataE11 to 201-01-00100!D4. Once that is done I want the macro to find the next template and carry on until the end of my list. Is that possible? Below is the current merge macro I am using. Thanks in advance

VBA Code:
Sub TestingMergeExcelFiles()
    
   Dim vFiles As Variant, vFile As Variant
    Dim wb As Workbook, ws As Worksheet
    Dim n As Long

    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    With ThisWorkbook.Worksheets("Summary")
        vFiles = Application.Transpose(.Range("A4:A" & .Range("A4").End(xlDown).Row).SpecialCells(xlCellTypeConstants))
    End With
    
    For Each vFile In vFiles
        Set wb = Workbooks.Open("C:\Users\d04m9\Desktop\test\" & vFile & ".xlsm")
        For Each ws In wb.Worksheets
            ws.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
            n = n + 1
        Next
        wb.Close False
    Next vFile
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    
    MsgBox "Processed " & UBound(vFiles) & " files" & vbCrLf & "Merged " & n & " worksheets", Title:="Merge Excel files"
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I have the wrong worksheet and range data in my vba code on the previous post. Below is the corrected code.

VBA Code:
Sub TestingMergeExcelFiles()
    
   Dim vFiles As Variant, vFile As Variant
    Dim wb As Workbook, ws As Worksheet
    Dim n As Long

    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    With ThisWorkbook.Worksheets("Data")
        vFiles = Application.Transpose(.Range("C11:C" & .Range("C11").End(xlDown).Row).SpecialCells(xlCellTypeConstants))
    End With
    
    For Each vFile In vFiles
        Set wb = Workbooks.Open("C:\Users\d04m9\Desktop\test\" & vFile & ".xlsm")
        For Each ws In wb.Worksheets
            ws.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
            n = n + 1
        Next
        wb.Close False
    Next vFile
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    
    MsgBox "Processed " & UBound(vFiles) & " files" & vbCrLf & "Merged " & n & " worksheets", Title:="Merge Excel files"
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,938
Members
448,534
Latest member
benefuexx

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