Custom Horizontal Sort (VBA)

lneidorf

Board Regular
Joined
May 20, 2004
Messages
97
Office Version
  1. 365
Platform
  1. Windows
Hi there.

I've put together a small bit of code that applies a custom sort order horizontally, which is defined by the contents of a range of cells on another tab.

Here's my code:
Code:
Sub Custom_Sort()

With Sheets("Data")
   Application.AddCustomList ListArray:=Sheets("Sort Order").Range("A2:A99")   
   Varlist = Application.CustomListCount
   Varlist = Varlist + 1
   
   .Range("A1").CurrentRegion.Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
   OrderCustom:=Varlist, MatchCase:=False, Orientation:=xlLeftToRight
   'Delete list
   Application.DeleteCustomList (Varlist - 1)
End With

End Sub

Here's how this works at present:

A) On the "Sort Order" tab, I list the header names of columns I want to sort horizontally. They appear in column A, starting at A2, in the order that they should be sorted. Cell A1 is a header cell not involved in the custom sort list.

B) On the "Data" tab, I paste the raw export of an extensive SQL report, containing many more columns than I need.

C) This macro is meant to sort the contents of the "Data" sheet according to the header names listed in column A of the "Sort Order" tab.


My code works. But there are two enhancements I'd like to make here:

1) My code to define the range that is used to create the custom sort list is static [.Range("A2:A99")]. I'd like to make this range dynamic, beginning at cell A2 and extending down column A until the last contiguous used cell. I've tried things like the following to no effect:
Code:
   Application.AddCustomList ListArray:=Sheets("Sort Order").Range("A2").End(xlDown)

2) On the "Data" tab, I'd like to delete any columns whose header values are not in my list of sort values on the "Sort Order" tab.

Any ideas would be most appreciated!

Thanks.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Give this a try for the first part...

Code:
Application.AddCustomList ListArray:=Sheets("Sort Order").Range("A2" & Rows.Count).End(xlUp).Row.End(xlUp)

I hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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