In my previous blog post I demonstrated how to transpose data from one workbook to another based on matching header names. Today, I want to illustrate how to reorder columns (within the same workbook) based on header names. I will use the same date set as last week.
The goal is to reorder columns alphabetically based on the header names.
STEP 1] Copy all header names and sort them in the desired order (i.e. from A to Z).
STEP 2] Use the site below to generate a comma-separated string of the header names.
STEP 3] Copy and paste the string created in step 2 in the 6th line of the code below (between the parenthesis ().
Dim ColumnOrder As Variant, DR As Integer
Dim Found As Range, counter As Integer
'Place the column headers in the end result order you want.
ColumnOrder=Array("Date","Audio Equipment","Beauty Supplies","Cameras","Footwear","Furniture","Jewelry","Luggage","Petcare","Smartphones","Sportswear","Swimsuits","Womenswear")
counter = 1
Application.ScreenUpdating = False
For DR = LBound(ColumnOrder) To UBound(ColumnOrder)
Set Found = Rows("1:1").Find(ColumnOrder(DR), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Not Found Is Nothing Then
If Found.Column <> counter Then
Application.CutCopyMode = False
counter = counter + 1
Application.ScreenUpdating = True
STEP 4] Copy and paste the code above into the VBA editor. Then, run the macro.
Here is the desired outcome with columns reordered in alphabetical sequence.