Sorting code brings up 1004 Application-defined or Object Defined Error

Frankietheflyer

New Member
Joined
Nov 17, 2017
Messages
30
The following bit of code brings up the above 1004 error when it gets to .Apply, but for the life of me I can't work out why!
Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub DataSort()[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Dim wsRes As Worksheet, wsPoi As Worksheet[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Set wsRes = ThisWorkbook.Sheets("Results")
Set wsPoi = ThisWorkbook.Sheets("Title Points")[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Application.ScreenUpdating = False
    
                                'Sort Results
 
 With wsRes.Sort
    .SortFields.Add Key:=Range("AN:AN"), Order:=xlAscending
    .SortFields.Add Key:=Range("AM:AM"), Order:=xlAscending
    .SetRange Range("AL1:AO20")
    .Header = xlYes
    .Apply
  End With
  
                                 'Sort Number of wins against each Team
      
   With wsRes.Sort
    .SortFields.Add Key:=Range("AA:AA"), Order:=xlDescending
    .SetRange Range("AA1:AD6000")
    .Header = xlYes
    .Apply
   End With
   
                                 'Sort Title Points
   
   With wsPoi.Sort
    .SortFields.Add Key:=Range("B:B"), Order:=xlDescending
    .SetRange Range("A1:C600")
    .Header = xlYes
    .Apply
   End With
   
Application.ScreenUpdating = True[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]End Sub
[/FONT]

(The AA1:AD6000 bit are columns of variable length but with several blank rows in so unless I give it specific "length" it only sorts to the first blank)

Can anyone give me an idea of why it throws up the 1004 please?

Many thanks

Frankie
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You need to clear the sortfields in between.
 
Upvote 0
I mean this:

Rich (BB code):
Sub DataSort()
Dim wsRes As Worksheet, wsPoi As Worksheet
Set wsRes = ThisWorkbook.Sheets("Results")
Set wsPoi = ThisWorkbook.Sheets("Title Points")
Application.ScreenUpdating = False
    
                                'Sort Results
 
 With wsRes.Sort
    .Sortfields.Clear
    .SortFields.Add Key:=Range("AN:AN"), Order:=xlAscending
    .SortFields.Add Key:=Range("AM:AM"), Order:=xlAscending
    .SetRange Range("AL1:AO20")
    .Header = xlYes
    .Apply
  End With
  
                                 'Sort Number of wins against each Team
      
   With wsRes.Sort
    .Sortfields.Clear
    .SortFields.Add Key:=Range("AA:AA"), Order:=xlDescending
    .SetRange Range("AA1:AD6000")
    .Header = xlYes
    .Apply
   End With
   
                                 'Sort Title Points
   
   With wsPoi.Sort
    .Sortfields.Clear
    .SortFields.Add Key:=Range("B:B"), Order:=xlDescending
    .SetRange Range("A1:C600")
    .Header = xlYes
    .Apply
   End With
   
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,953
Members
448,535
Latest member
alrossman

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