Sub Maybe()
Dim c As Range, headerArr, i As Long
headerArr = Array("Name", "Age") '<----- extend as far as you need
For i = LBound(headerArr) To UBound(headerArr)
For Each c In Range(Cells(1, 1), Cells(1, Cells(Columns.Count).End(xlToLeft).Column))
If c.Value = headerArr(i) Then Range(Cells(1, c.Column), Cells(c.End(xlDown).Row, c.Column)).Copy Sheets("Sheet2").Cells(1, Sheets("Sheet2").Cells(1, Columns.Count).End(xlToLeft).Column + 1): Exit For
Next c
Next i
End Sub
Change references where required
Slight difference.
Sub Maybe_A()
Dim headerArr, sh1 As Worksheet, sh2 As Worksheet, cCol As Long, i As Long, nc As Long
headerArr = Array("Name", "Age", "Address") '<---- extend as far as required
Set sh1 = Worksheets("Data")
Set sh2 = Worksheets("dont_touch")
For i = LBound(headerArr) To UBound(headerArr)
With sh1
cCol = .Rows(1).Find(headerArr(i)).Column
nc = IIf(WorksheetFunction.CountA(sh2.Rows(1)) = 0, 1, sh2.Cells(1, sh2.Columns.Count).End(xlToLeft).Column + 1)
.Range(.Cells(1, cCol), .Cells(.Cells(.Rows.Count, cCol).End(xlUp).Row, cCol)).Copy sh2.Cells(1, nc)
End With
Next i
End Sub
Bookmarks