Amend code to add a criteria to a 'sort'

Mel Smith

Well-known Member
Joined
Dec 13, 2005
Messages
1,023
Office Version
  1. 365
Platform
  1. Windows
I am using the following code, which works well, but now I find that I need to add a criteria to the sort. The code sorts column AJ smallest to largest but now I would like to perform the same sort but cells in the range that are zero need to be placed last, rather than after being sorted ending up at the top of the list.

My code is:
VBA Code:
    lr = Cells(Rows.Count, "AJ").End(xlUp).Row - 8
   
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add2 Key:= _
        Range("AJ4:AJ" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("AE3:AQ" & lr)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Many thanks.
Mel
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi do you mean you need the sort to be DEscending, rather than Ascending ? (so biggest to smallest therefore, which puts zeros at the bottom ?

You could just ammend this part of the code I think :

Excel Formula:
Order:=xlAscending
to
Excel Formula:
 Order:=xlDescending
 
Upvote 0
Hi Rob,

No, I still need the sort to be Ascending but with the added criteria that cells in the sort range that are zero are list last rather than first. something like: 1, 2, 3, 4 etc then 0, 0, 0

Mel
 
Upvote 0
I thought too easy to be true.. :)

So the way I have done it is to use a "helper" column in your data, which will contain 1 for numbers greater than zero, and a 0 for the rows containing well.. a zero.

So I used (for my example) Column AL, and inserted a formula to get all the helper data down the column:
Excel Formula:
=IF(AJ4>0,1,0)
which basically when copied down your data gives you the 1's and 0's needed.

Then, after your initial sort, I just sorted again based on this column AL, but in a Descending manor. There might be some cleaner code out there, but this worked for me anyway by just inserting it after your initial sort.

Of course, you may have data in Col AL - so use whichever column you see fit.

VBA Code:
ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add2 Key:= _
        Range("AL4:AL" & lr), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("AE3:AQ" & lr)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
Assuming that the values in column AJ are numerical and not the result of formulas then another option might be to
- include in your code before the sort a Find/Replace to replace 0 with "z"
- do the sort as you already have it
- include in your code after the sort a Find/Replace to replace "z" with 0
 
Upvote 0
Hello Peter,

I think your idea will work better for me. How can I amend my code to Find/Replace my zeros to "z" and vice versa?

Mel
 
Upvote 0
I don't have your whole code or sample data to test, but assuming lr correctly identifies the last row to operate on try something like this

Rich (BB code):
Line that evaluates lr

Range("AJ4:AJ" & lr).Replace What:=0, Replacement:="z", LookAt:=xlWhole

The rest of your sorting code, assuming that is working

Range("AJ4:AJ" & lr).Replace What:="z", Replacement:=0, LookAt:=xlWhole
 
Upvote 0
Solution
Hello Peter,

Thank you for your code, which partially worked. For some reason it didn't move all the zero records to the bottom but rather grouped them together in the middle of the table, which seems rather odd. I'm trying to work out why.

Mel
 
Upvote 0
Peter,

Below is the befgore and after.

Mel
Book2
ABCDEFGHIJKLM
1Before:
2OvsMdnsRunsWktsAveBestStrike rateEconomy5 wkt innings10 wkt matchWide balls bowledNo balls bowled
30.000/00.000.0000
432.021360.000/610.004.2500
5409.38313193142.554/3179.263.22005(17)
6241.1338991849.943/2680.393.730011(22)
76.2317117.001/1738.002.6800
81.010.000/10.001.0000
921.0578178.001/16126.003.7100
100.140.000/00.0024.00002(4)
11472.214112565323.706/4053.472.66302(2)4(8)
1215.0160160.001/5490.004.0000
13186.5277241165.824/85101.913.88003(8)3(6)
146.5350.000/10.005.1200
15116.5303582216.279/6231.863.06212(5)
16180.039559869.882/44135.003.11002(2)6(21)
170.000/00.000.0000
187.057157.001/3942.008.14001(2)
1929.15100250.001/487.503.4300
20203.3307572037.854/8561.053.72001(5)3(7)
2128.41137268.501/6486.004.7800
2240.014114619.005/8240.002.8510
23100.5182741222.835/9050.422.72102(2)3(6)
24TOTALS846.443268851890.00---0.000.007115(36)35(81)
25
26After:
27OvsMdnsRunsWktsAveBestStrike rateEconomy5 wkt innings10 wkt matchWide balls bowledNo balls bowled
28116.5303582216.279/6231.863.06212(5)
296.2317117.001/1738.002.6800
30472.214112565323.706/4053.472.66302(2)4(8)
31409.38313193142.554/3179.263.22005(17)
32241.1338991849.943/2680.393.730011(22)
3315.0160160.001/5490.004.0000
34186.5277241165.824/85101.913.88003(8)3(6)
35180.039559869.882/44135.003.11002(2)6(21)
3621.0578178.001/16126.003.7100
370.000/00.000.0000
3832.021360.000/610.004.2500
391.010.000/10.001.0000
400.140.000/00.0024.00002(4)
416.5350.000/10.005.1200
420.000/00.000.0000
437.057157.001/3942.008.14001(2)
4429.15100250.001/487.503.4300
45203.3307572037.854/8561.053.72001(5)3(7)
4628.41137268.501/6486.004.7800
4740.014114619.005/8240.002.8510
48100.5182741222.835/9050.422.72102(2)3(6)
49TOTALS846.443268851890.00---0.000.007115(36)35(81)
Sheet1
Cell Formulas
RangeFormula
C24:E24,J49:K49,C49:E49,J24:K24C24=SUM(C3:C23)
F24,H49:I49,F49,H24:I24F24=MIN(F3:F23)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E28:E48Expression=E28=MAXIFS($AI$4:$AI$24,$AI$4:$AI$24,">"&0)textNO
F28:F48Expression=F28=MINIFS($AJ$4:$AJ$24,$AJ$4:$AJ$24,">"&0)textNO
H28:H48Expression=H28=MINIFS($AL$4:$AL$24,$AL$4:$AL$24,">"&0)textNO
I28:I48Expression=I28=MINIFS($AM$4:$AM$24,$AM$4:$AM$24,">"&0)textNO
E3:E23Expression=E3=MAXIFS($AI$4:$AI$24,$AI$4:$AI$24,">"&0)textNO
F3:F23Expression=F3=MINIFS($AJ$4:$AJ$24,$AJ$4:$AJ$24,">"&0)textNO
H3:H23Expression=H3=MINIFS($AL$4:$AL$24,$AL$4:$AL$24,">"&0)textNO
I3:I23Expression=I3=MINIFS($AM$4:$AM$24,$AM$4:$AM$24,">"&0)textNO
 
Upvote 0

Forum statistics

Threads
1,214,613
Messages
6,120,515
Members
448,968
Latest member
Ajax40

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