VBA to merge data from multiple sheets along with sheet name

aaleem

Board Regular
Joined
Sep 26, 2014
Messages
56
Office Version
  1. 2016
Hi,

i have the below VBA code which merges the data from multiple sheets in a workbook and arranges the data based on the column headers. i would like to amend this code to add the sheet name as well in column BH. any help will be much appreciated.

VBA Code:
Sub CopyHeaders()
    Dim header As Range, headers As Range
    Dim ws2 As Worksheet
    Dim Template As Worksheet
    Dim cell As Range
    
    For Each ws2 In ActiveWorkbook.Worksheets
    If IsError(Application.Match(ws2.Name, _
    Array("Template", "Sheet1"), 0)) Then
    Set Rng = ws2.UsedRange
    For Each cell In Rng
      If cell.Value = "" Then cell.Value = "0"

    Next
    Set headers = ws2.Range("A1:bg1")
    For Each header In headers
        If GetHeaderColumn(header.Value) > 0 Then
        Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=Worksheets("Template").Cells(Worksheets("Template").Rows.Count, GetHeaderColumn(header.Value)).End(xlUp).Offset(1, 0)
        
        End If
    
    Next
    End If
    Next
End Sub
Function GetHeaderColumn(header As String) As Integer
    Dim headers As Range
    Set headers = Worksheets("Template").Range("A1:bg1")
    GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function


thanks
aleem
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How about
VBA Code:
    For Each header In headers
        If GetHeaderColumn(header.Value) > 0 Then
            NxtRw = Worksheets("Template").Cells(Worksheets("Template").Rows.Count, GetHeaderColumn(header.Value)).End(xlUp).Offset(1).Row
            With Range(header.Offset(1, 0), header.End(xlDown))
               .Copy Worksheets("Template").Cells(NxtRw, GetHeaderColumn(header.Value))
               Worksheets("Template").Cells(NxtRw, "BM").Resize(.Rows.Count).Value = ws2.Name
            End With
        
        End If
    
    Next
 
Upvote 0
Solution
How about
VBA Code:
    For Each header In headers
        If GetHeaderColumn(header.Value) > 0 Then
            NxtRw = Worksheets("Template").Cells(Worksheets("Template").Rows.Count, GetHeaderColumn(header.Value)).End(xlUp).Offset(1).Row
            With Range(header.Offset(1, 0), header.End(xlDown))
               .Copy Worksheets("Template").Cells(NxtRw, GetHeaderColumn(header.Value))
               Worksheets("Template").Cells(NxtRw, "BM").Resize(.Rows.Count).Value = ws2.Name
            End With
       
        End If
   
    Next
Thank you so much Fluff, Excellent piece of code, it works perfectly. below is the final code, i just change the destination cell to BH to suit my data table
VBA Code:
Sub CopyHeaders()
    Dim header      As Range, headers As Range
    Dim ws2         As Worksheet
    Dim Template    As Worksheet
    Dim cell        As Range
    
    For Each ws2 In ActiveWorkbook.Worksheets
        If IsError(Application.Match(ws2.Name, _
           Array("Template", "Sheet1"), 0)) Then
        Set Rng = ws2.UsedRange
        For Each cell In Rng
            If cell.Value = "" Then cell.Value = "0"
            
        Next
        Set headers = ws2.Range("A1:BG1")
        
        For Each header In headers
        If GetHeaderColumn(header.Value) > 0 Then
            NxtRw = Worksheets("Template").Cells(Worksheets("Template").Rows.Count, GetHeaderColumn(header.Value)).End(xlUp).Offset(1).Row
            With Range(header.Offset(1, 0), header.End(xlDown))
               .Copy Worksheets("Template").Cells(NxtRw, GetHeaderColumn(header.Value))
               Worksheets("Template").Cells(NxtRw, "BH").Resize(.Rows.Count).Value = ws2.Name
            End With
        
        End If
    
    Next
        
        
        
    End If
Next

End Sub
Function GetHeaderColumn(header As String) As Integer
    Dim headers     As Range
    Set headers = Worksheets("Template").Range("A1:BG1")
    GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
    
End Function
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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