Sub vbax_54257_Open_WB_Copy_Cols_Changing_Order()
Dim fName As String, fPath As String
Dim calc As Long, ColNum As Long, i As Long
Dim LastRowCo As Long, LastColCo As Long, LastRowAc As Long
Dim ColHeads
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
calc = .Calculation
.Calculation = xlCalculationManual
.AskToUpdateLinks = False
End With
With Worksheets("Collation")
.Range("A2:C" & Rows.Count).ClearContents
LastColCo = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
ColHeads = Application.Transpose(Application.Transpose(.Range(.Cells(1, 1), .Cells(1, LastColCo)).Value))
'or assign it directly; if it is static:
'ColHeads = Array("Date", "Sales", "Vendor")
End With
fPath = "C:\Users\aravindhan.jayaraman\Desktop\Work Related\Automations & Simplifications\Rajesh\Formatting\Sales\test\"
fName = Dir(fPath & "*.xls*")
Do While Len(fName) > 0
LastRowCo = Worksheets("Collation").Cells.Find("*", , , , xlByRows, xlPrevious).Row
Workbooks.Open (fPath & fName)
With ActiveSheet
LastRowAc = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
For i = LBound(ColHeads) To UBound(ColHeads)
ColNum = .Rows(1).Find(ColHeads(i)).Column
.Range(.Cells(2, ColNum), .Cells(LastRowAc, ColNum)).Copy
ThisWorkbook.Worksheets("Collation").Cells(LastRowCo, i).PasteSpecial xlPasteValues
Next i
End With
ActiveWorkbook.Close False
fName = Dir
Loop
With Application
.EnableEvents = True
.Calculation = calc
.AskToUpdateLinks = True
End With
End Sub