Sorting a table - seems much harder than it should be?

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
243
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
In my workbook I have several sheets that contain tables in the same format.

I want to sort them by the columns in B and D - there are headers. B is a date, D is a number - so both in ascending sort order.

I adjusted some code I found and came up with this:

VBA Code:
  With WS2
        
        Set AllData = .UsedRange
        
        With .Sort
        
            .SortFields.Clear
            .SortFields.Add Key:=Range("B1"), SortOn:=xlSortValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SortFields.Add Key:=Range("D1"), SortOn:=xlSortValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange (Range(AllData))
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        
        End With

The main issues I'm having is in defining the range as it adjusts with every sheet (in terms of rows) so I tried to set it as UsedRange, but I keep getting errors in the .SetRange line depending on how I phrase the line.

As above, it says "Method "Range" of object global failed - but I also get requires object errors when I change the way I'm trying to refer to AllData as a range.

Do I really need to go in and define Last Row and Last Column Variables and define my range that way, using .Cells in the .SetRange line?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
AllData is a range, so you don't put it inside a Range call. But if you have a Table, you should be using the Listobject directly to manipulate it.
 
Upvote 0
I used "Table" in the general sense rather than the Excel sense - later on in the procedure the data gets converted to a table, so would it be better to do that at the start? It's easier to sort a ListObject?
 
Upvote 0
Easier, yes, but probably slower. Try just amending that code to:

Code:
.SetRange AllData
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

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