I have data in a dynamic range which I want to sort horizontally by the values in the 1st row. A macro fills a range, e.g. ("a2 to f12") with names in row 2 and data below, and then it pastes names from another sheet in row 1. The names in row 1 also appear in row 2 but in a different order.

Then I want to sort the data in the range by the names in row 1 as below:

The code I use is:
Dim sht As Worksheet
Set sht = ActiveSheet 'Sheet name: Data
Dim bottom As Long, right As Long
With sht
bottom = .Cells(2, 2).End(xlDown).Row
right = .Cells(2, 2).End(xlToRight).Column
End With
Application.AddCustomList ListArray:=Sheets("Data").Range(Cells(1, 1), Cells(1, right)), ByRow:=False
ActiveWorkbook.Worksheets("Data").Range(Cells(2, 1), Cells(bottom, right)).Sort Key1:=Range(Cells(2, 1), _ Cells(2, right)), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=Application.CustomListCount + 1, _ MatchCase:=False, Orientation:=xlLeftToRight, DataOption1:=xlSortNormal
Application.DeleteCustomList Application.CustomListCount
While the code usually works, sometimes it doesn't because the number of items can be several hundreds and it exceeds the limit of the custom list so, part of the data remains unsorted. Also, after I delete the custom list, Excel crashes. Is there any other way to sort my data without using a custom list?
Custom-Sort Rows
ActiveSheet; these cellsCells(bottom, right)are located in a worksheet, so qualify them.Rightis a VBA function. Surely you can make up your own variable name, e.g.rCol,cRight...Application.CustomListCount + 1shouldn't exist.ByRowargument, thedocumentationstates: "If this argument is omitted and there are more columns than rows in the range, Excel creates a custom list from each row in the range." This is true in our case: there are 6 columns and 1 row so "each row" is our only row hence one custom list will be added.