Error when sorting table - with exact same code that is working elsewhere

TheRedCardinal

Active Member
Joined
Jul 11, 2019
Messages
250
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I am doing some manipulation of a table that involves sorting it in various ways. At the end, I want to just go back to having it sorted by date, which is the column of data in column A.

Here is my code:

Excel Formula:
'Remove all filters and sorting

PurchTable.AutoFilter.ShowAllData

Set SortColumn = PurchTable.ListColumns(1).Range

With PurchTable.Sort

    .SortFields.Clear
    .SortFields.Add Key:=SortColumn, SortOn:=xlSortOnValues, Order:=Ascending, DataOption:=xlSortNormal
    .Apply

End With

When I run it, I get "Application-defined or object-defined error" at the .Sortfields Add line

Here is my code that does exactly the same with a different table:

VBA Code:
'Undo all filters and resort by date

Set SortColumn = SalesTable.ListColumns(1).Range

With SalesTable

    .AutoFilter.ShowAllData
    
End With

With SalesTable.Sort

    .SortFields.Clear
    .SortFields.Add Key:=SortColumn, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Apply
    .SortFields.Clear
    
End With

I cannot see what the difference is between these codes (other than minor tidying issues), and why one would run and why the other would not.

I have sense checked the references in the immediate window, and they all look right.

So what is the issue?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You dont say where/when the error is generated? Or maybe no error but just doesnt work?

You have Order:=xlAscending on the original and Order:=Ascending on the other.

Failing that, how are you defining SalesTable and PurchTable?
 
Upvote 0
Solution

Forum statistics

Threads
1,223,099
Messages
6,170,108
Members
452,302
Latest member
TaMere

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