Hello,
I am trying to move data from one worksheet to another worksheet. The columns I want to move are not in order. I copied one of you codes and made some minor changes.
Sub test()
Dim Headers As Variant
Dim ColumnNumbers As Variant
Dim i As Long
Dim SourceColumn As Range
Dim DestinationSheet As Worksheet
Set DestinationSheet = ThisWorkbook.Sheets("Sorted")
Headers = Array("Customer", "Tail Number", "Description", "Date", "Gallons", "Revenue", "Cost")
ColumnNumbers = Array(1, 2, "J", "K", "F", "G", "B", "C", "D")
For i = LBound(Headers) To UBound(Headers)
With ThisWorkbook.Sheets("FuelMarginTrendAnalysis").Rows(1)
Set SourceColumn = .Find(Headers(i), after:=.Cells(1, 1), MatchCase:=False)
End With
If Not SourceColumn Is Nothing Then
SourceColumn.EntireColumn.Copy Destination:=DestinationSheet.Columns(ColumnNumbers(i))
End If
Next i
End Sub
This is what I am getting. I appreciate the help.
[TABLE="width: 867"]
<tbody>[TR]
[TD]Customer ID[/TD]
[TD]Cost[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Gallons[/TD]
[TD]Revenue[/TD]
[TD][/TD]
[TD][/TD]
[TD]Description[/TD]
[TD]Date
[/TD]
[/TR]
</tbody><colgroup><col><col><col span="3"><col><col><col span="2"><col><col></colgroup>[/TABLE]
I am trying to move data from one worksheet to another worksheet. The columns I want to move are not in order. I copied one of you codes and made some minor changes.
Sub test()
Dim Headers As Variant
Dim ColumnNumbers As Variant
Dim i As Long
Dim SourceColumn As Range
Dim DestinationSheet As Worksheet
Set DestinationSheet = ThisWorkbook.Sheets("Sorted")
Headers = Array("Customer", "Tail Number", "Description", "Date", "Gallons", "Revenue", "Cost")
ColumnNumbers = Array(1, 2, "J", "K", "F", "G", "B", "C", "D")
For i = LBound(Headers) To UBound(Headers)
With ThisWorkbook.Sheets("FuelMarginTrendAnalysis").Rows(1)
Set SourceColumn = .Find(Headers(i), after:=.Cells(1, 1), MatchCase:=False)
End With
If Not SourceColumn Is Nothing Then
SourceColumn.EntireColumn.Copy Destination:=DestinationSheet.Columns(ColumnNumbers(i))
End If
Next i
End Sub
This is what I am getting. I appreciate the help.
[TABLE="width: 867"]
<tbody>[TR]
[TD]Customer ID[/TD]
[TD]Cost[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Gallons[/TD]
[TD]Revenue[/TD]
[TD][/TD]
[TD][/TD]
[TD]Description[/TD]
[TD]Date
[/TD]
[/TR]
</tbody><colgroup><col><col><col span="3"><col><col><col span="2"><col><col></colgroup>[/TABLE]