VBA to sort range excluding zero values

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

I'd be grateful for some code in a module that will sort range 'Daily Tracking'!C429:D508 (which changes order regularly) into ascending order, but ignoring all the zero values, as below.

52013
72017
122011
142016
291981
631982
1042014
1192015
1332012
1382018
2611997
2671993
3262010
3321995
3401984
3472020
4132001
4222022
5201996
5301985
5331990
5381999
5791998
5982021
6461992
7851991
9311988
9642009
9792019
11001989
11252000
11532002
12162008
12881987
13171994
13282006
13622003
15492005
17511986
20902007
23072004
02023
02024
02025
02026
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041
02042
02043
02044
02045
02046
02047
02048
02049
02050
02051
02052
02053
02054
02055
02056
02057
02058
02059
02060
02061


Many thanks!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Perhaps this.

VBA Code:
Sub SortRange()
    Dim WS As Worksheet
    Dim SortRange As Range

    Set WS = Worksheets("Daily Tracking")
    Set SortRange = WS.Range("C429:D508")

    If SortRange.Rows.Count > 1 Then
        SortRange.Sort Key1:=SortRange.Columns("A"), Order1:=xlDescending
   
        With Application.WorksheetFunction
            Set SortRange = SortRange.Resize(.Count(SortRange.Resize(, 1)) - .CountIf(SortRange.Resize(, 1), 0))
        End With

        SortRange.Sort Key1:=SortRange.Columns("A"), Order1:=xlAscending
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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