TheRedCardinal
Active Member
- Joined
- Jul 11, 2019
- Messages
- 252
- Office Version
- 365
- 2021
- Platform
- 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:
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:
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?
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?