I am naive to macros and i tried the below code to copy data based on column header from workbook1 to workbook2. But it copies only the rows with data. I have some blank rows in between. Any help will be much appreciated.
I presume the End(x1Down).Copy is stopping to proceed further. Instead I tried using EntireColumn.Copy, but it spolis the formating in the destination workbook.
Sub CopyHeaders()
Dim header As Range, headers As Range
Set headers = Worksheets("ws1").Range("A1:Z1")
For Each header In headers
If GetHeaderColumn(header.Value) > 0 Then
Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=Worksheets("ws2").Cells(2, GetHeaderColumn(header.Value))
End If
Next
End Sub
Function GetHeaderColumn(header As String) As Integer
Dim headers As Range
Set headers = Worksheets("ws2").Range("A1:Z1")
GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)),Application.Match(header, headers, 0), 0)
End Function