Sort a column in descending order, putting strings at bottom

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
275
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2011
Platform
  1. Windows
  2. MacOS
I sort a column in descending order via VBA code. Sometimes there's a string value in the column that begins with a number. It always put that value at the top when it sorts in descending order. Is there a way to get it to put that at the bottom instead? I tried to do it manually, but could not find how to do it.

Here's an example of what my column might look like.
Screenshot (189).png


I then told it to Sort, like this:
Screenshot (190).png


When I clicked OK, this is what is did:
Screenshot (191).png


I would like "2nd game" to be at the bottom. Is there a way to tell Excel to put all of the non-numeric values at the bottom of the sort, even in descending order?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you're OK with a VBA solution, then try the following (assumes your data is exactly as you've shown in your original post).

VBA Code:
Sub SortTwice()
    With Range("A1").CurrentRegion.Resize(, 2)
        .Columns(2).FormulaR1C1 = "=if(isnumber(rc1),1,0)"
        .Sort Key1:=.Cells(1, 2), order1:=xlDescending, key2:=.Cells(1, 1), order2:=xlDescending, Header:=xlNo
        .Columns(2).ClearContents
    End With
End Sub
 
Upvote 0
If you're OK with a VBA solution, then try the following (assumes your data is exactly as you've shown in your original post).

VBA Code:
Sub SortTwice()
    With Range("A1").CurrentRegion.Resize(, 2)
        .Columns(2).FormulaR1C1 = "=if(isnumber(rc1),1,0)"
        .Sort Key1:=.Cells(1, 2), order1:=xlDescending, key2:=.Cells(1, 1), order2:=xlDescending, Header:=xlNo
        .Columns(2).ClearContents
    End With
End Sub
Ah... I see what you did there. So basically, I need a "helper" column to notate which columns are numeric and which are not.

The columns on my actual sheet are slightly different, but I see what the code is doing. I'll give that a shot. Thanks!
 
Upvote 0
If you wanted to do it without a helper column you could test this.
I have assumed
  • That you would not be doing this if there were no straight numbers so I have not done an error check for that.
  • That the numbers are not the result of formulas
VBA Code:
Sub Sort_Special()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlNo
    .SpecialCells(xlConstants, xlNumbers).Sort Key1:=.Cells(1), Order1:=xlDescending, Header:=xlNo
  End With
End Sub

You could of course do the same thing manually in 2 steps.
Select all the data and sort Ascending
Select the numbers only, which will now be grouped together, & sort Descending

Before:

mcomp72.xlsm
A
15
2xyz
37
41
53
69
72nd game
8abc
911
102
116
Sheet1


After:

mcomp72.xlsm
A
111
29
37
46
55
63
72
81
92nd game
10abc
11xyz
Sheet1
 
Last edited:
Upvote 0
If you wanted to do it without a helper column you could test this.
I have assumed
  • That you would not be doing this if there were no straight numbers so I have not done an error check for that.
  • That the numbers are not the result of formulas
VBA Code:
Sub Sort_Special()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlNo
    .SpecialCells(xlConstants, xlNumbers).Sort Key1:=.Cells(1), Order1:=xlDescending, Header:=xlNo
  End With
End Sub

Before:

mcomp72.xlsm
A
15
2xyz
37
41
53
69
72nd game
8abc
911
102
116
Sheet1


After:

mcomp72.xlsm
A
111
29
37
46
55
63
72
81
92nd game
10abc
11xyz
Sheet1
Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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