I'd like to copy rows of data from two worksheets ('8105', '9038') to match the column headings in the destination worksheet ('8105 + 9038'). I'm using code because the columns are not in the same order across all three worksheets, and there are many cells.
At the moment, the code I have works for taking data from one worksheet (8105). I'd like to edit the code so that data is taken from the worksheet '8105' and another worksheet titled '9038'. In the worksheet '9038', the headers go from A to AM.
Any guidance would be much appreciated!
At the moment, the code I have works for taking data from one worksheet (8105). I'd like to edit the code so that data is taken from the worksheet '8105' and another worksheet titled '9038'. In the worksheet '9038', the headers go from A to AM.
Any guidance would be much appreciated!
Code:
Sub CopyHeaders()
Dim header As Range, headers As Range
Set headers = Worksheets("8105").Range("A1:AK1")
For Each header In headers
If GetHeaderColumn(header.Value) > 0 Then
Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=Worksheets("8105 + 9038").Cells(2, GetHeaderColumn(header.Value))
End If
Next
End Sub
Function GetHeaderColumn(header As String) As Integer
Dim headers As Range
Set headers = Worksheets("8105 + 9038").Range("A1:AK1")
GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function