Sort Table by Multiple Keys - VBA

melodramatic

Board Regular
Joined
Apr 28, 2003
Messages
187
Office Version
  1. 365
Platform
  1. Windows
I am having a tough time getting a sort in my macro. Here's what I currently have:

Code:
        Range("Table1[#All]").Select
            ActiveWorkbook.Worksheets("Entry").Sort Key1:=Range("Table1[[#All],[DCN]]"), _
                Order1:=xlAscending, _
            Key2:=Range("Table1[#All],[Date]]"), _
                Order2:=xlAscending, _
                SortOn:=xlSortOnValues, _
                DataOption2:=xlSortTextAsNumbers, _
                Header:=xlYes

Which gives me the "Run-time error '1004'" Method 'Range' of object '_Global' failed.

If I run the sort manually, here's what I'm trying to do:

1677253640745.png


Can someone help me figure out what I'm doing wrong?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi melodramatic,

I think you have to do a ".Sort.SortFields.Add2" for each key. I could be mistaken. Maybe record a macro sorting two levels to see what the recorder does. Undoubtedly, you will have to clean-up the code.

I hope that helps,

Doug
 
Upvote 0
You are mixing up the two different types of sort, try
Excel Formula:
        Range("Table1[#All]").Sort Key1:=Range("Table1[DCN]"), _
                Order1:=xlAscending, _
            Key2:=Range("Table1[Date]"), _
                Order2:=xlAscending, _
                dataoption1:=xlSortOnValues, _
                DataOption2:=xlSortTextAsNumbers, _
                Header:=xlYes
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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