Sort code setting criteria but NOT SORTING

aereeves

New Member
Joined
Jul 2, 2009
Messages
5
When I run the following piece of code in Excel 2007 the sort fields are being added (I say that because a manual sort after running the code contains these criteria), but the data is not actually sorted. Suggestions much appreciated. Thanks. Alicia

With ActiveWorkbook.Worksheets("Salary").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("A25:A49"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("G25:G49"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange Range("A25:BU49")
.Orientation = xlTopToBottom
.Apply
End With
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi,

Try qualifying those range properties with the correct parent sheet.

For example:

Rich (BB code):
     Dim wstSalary As Worksheet
     
     Set wstSalary = ActiveWorkbook.Worksheets("Salary")
     
     
     With wstSalary.Sort
          .SortFields.Clear
          
          .SortFields.Add Key:=wstSalary.Range("A25:A49"), _
                         SortOn:=xlSortOnValues, _
                         Order:=xlDescending, _
                         DataOption:=xlSortNormal
          
          .SortFields.Add Key:=wstSalary.Range("G25:G49"), _
                         SortOn:=xlSortOnValues, _
                         Order:=xlDescending, _
                         DataOption:=xlSortNormal
          
          .SetRange wstSalary.Range("A25:BU49")
          .Orientation = xlTopToBottom
          .Apply
     End With

Hope that helps...
 
Upvote 0
Thanks for your reply, Colin. Unfortunately the sort still isn't working. I modified my code as shown below. When I run the subroutine (without any breaks) nothing seems to happen (no sorting). If I set a breakpoint and step through this section of the code then I get a run time error ('1004') when I get to the .Apply line.

Any thoughts out there?

Dim wks5YP as Worksheet

Set wks5YP = ActiveWorkbook.Worksheets("Salary")
With wks5YP.Sort
.SortFields.Clear
.SortFields.Add Key:=wks5YP.Range("A25:A49"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SortFields.Add Key:=wks5YP.Range("G25:G49"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange wks5YP.Range("A25:BV49")
.Orientation = xlTopToBottom
.Apply
End With
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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