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
 

Some videos you may like

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Colin Legg

MrExcel MVP, Like totally RAD man
Joined
Feb 28, 2008
Messages
3,497
Office Version
365, 2016
Platform
Windows
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...
 

aereeves

New Member
Joined
Jul 2, 2009
Messages
5
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
 

Watch MrExcel Video

Forum statistics

Threads
1,101,870
Messages
5,483,434
Members
407,395
Latest member
Sakshine

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top