i have workbook, where i need a macro that he finds columns with same headers, and he copies data new sheet. colomns in different order in sheets. this is my code, he works with the sheets that that have same order of columns
VBA Code:
Sub Merge_Sheets()
Dim startRow, startCol, lastRow, lastCol As Long
Dim headers As Range
Dim ws As Worksheet
Dim pas As Worksheet
'Set Master sheet for consolidation
Set wb = ActiveWorkbook
Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = "AllSheets"
Set mtr = Worksheets("AllSheets")
Sheets("Sheet1").Activate
'Get Headers
Set headers = Application.InputBox("Izaberi opseg Header-a", Type:=8)
'Copy Headers into master
headers.Copy mtr.Range("A1")
startRow = headers.Row + 1
startCol = headers.Column
Debug.Print startRow, startCol
'loop through all sheets
For Each ws In wb.Worksheets
'except the master sheet from looping
If ws.Name <> "AllSheets" Then
ws.Activate
lastRow = Cells(Rows.Count, startCol).End(xlUp).Row
lastCol = Cells(startRow, Columns.Count).End(xlToLeft).Column
'get data from each worksheet and copy it into AllSheets sheet
Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy _
mtr.Range("A" & mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1)
End If
Next ws
Sheets("AllSheets").Activate
End Sub