Sorting Columns via headers.

Rado88

New Member
Joined
Dec 30, 2017
Messages
45
Hi All,

I've a query regarding sorting columns via headers in few sheets of one workbook. The data to sheets 2 + will be copied daily but with same columns (in different order).

I've a workbook with few sheets full of data, and all have same columns in different order (for example column C from sheet 1 is column Z (with different data in rows) in sheet 2)). Sheet 1 has the correct order in which the fle should look to make other macros to work. There can be up to 3 - 4 sheets that need to be adjusted to sheet1.

I was wondering if it's possible to write a macro that would loop through column headers and move them to same order as in sheet1. I was thinking about using ListObject.HeaderRowRange Property but I'm not sure how to start. I know that the fastest solution would be to record a macro, but the columns end on BH or further column, and if the columns would be in different order then on the one used to record the macro this could cause issues.

Thank you in advance for any tips how to create such sorting tool.
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Code:
Sub SortCol()
Dim master$, ray As Variant, ListNum%, sh As Worksheet
master = "Sheet1" 'Change to actual master sheet name
ListNum = Application.CustomListCount + 2
With Sheets(master)
    ray = .Range(.[A1], .Cells(1, Columns.Count).End(xlToLeft))
End With
Application.AddCustomList ray
For Each sh In Worksheets
    If sh.Name <> master Then _
        sh.Cells.Sort key1:=sh.Rows(1), Header:=xlNo, _
        Orientation:=xlLeftToRight, OrderCustom:=ListNum
Next
ListNum = ListNum - 1
Application.DeleteCustomList ListNum
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,957
Members
449,200
Latest member
indiansth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top