Using RANK on a range of data - need an exception

BlondieC

New Member
Joined
Feb 9, 2016
Messages
41
Hi, this is the last piece for a little tool I'm working on. I've used RANK for the data range but what I need is for any cell with 0.00 in Column E to remain with that cell content and not be ranked numerically and preferablyy be at the bottom of the data content.

Only cells >0.00 should be ranked highest to lowest based on the revenue dollar amount.

The idea is to have the ranking number displayed rather that the revenue dollar amount except where the revenue = 0.00. Currently the data is ranking the way it should and displays the way it should but the rows with 0.00 are also displaying a ranking number. Thank you

My code:

Code:
Sheets("SiteCopy").Select
    Range("E8:E" & Range("E7").End(xlDown).Offset(-1).Row).Name = "DataRank"
       
    With Range("DataRank").Offset(0, 1)
        .FormulaArray = "=RANK(DataRank,DataRank)"
    
End With
 
Last edited:
I do not remember what I did ;)
I have to start from scratch. Try to show us a new sample of data (where the current code fails), the expected results and the logic behind.
I have to leave now. I'll try to take a look later (tomorrow).
In the meantime maybe someone else can help.

M.
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I know that feeling! Here is the code that works perfectly as long as there is more than one line to be ranked. I have comments next to two lines in the code.

Code:
Dim LastRow As Long    
    With Sheets("SiteCopy")
        LastRow = .Cells(8, "C").End(xlDown).Row
        .Range("E7") = "Ranking"
        With Range("F8:F" & LastRow)
            .Formula = "=IF(E8=0,E8,RANK(E8,E$8:E$" & LastRow & "))"   '*****Issue with this line: it ranks the one line of data 1 (which is correct) and then ranks the Revenue Total dollar amount directly below the business also as 1.  It also then results in a 0 being entered in all remaining cells in column F
            .Value = .Value
        End With
        .Range("B7:F" & LastRow).Sort key1:=.Range("E7"), order1:=xlDescending, Header:=xlYes
        .Range("F" & LastRow + 1) = .Range("E" & LastRow + 1)   '*******The code fails here when there is only one line of data or one business to rank and this is where I get the Runtime error 1004
        .Columns("E:E").Delete
        
        'Optional: formats last two cells
        .Range("E" & lastRow & ":E" & lastRow).NumberFormat = "[$$-409]#,##0.00"
    End With


This is the output produced - Because there is only one line of data it's ranking the total line also and then drops 0 into all remaining cells in column F:
Site CCSite NameBusiness NameRanking
1038LOWER JAMESTORONTO24309.251
Revenue Total24309.251
0
0
0
0
0
0
0

<tbody>
</tbody>


These are the results when there is more than one line of data to rank and this is the desired and correct outcome:
Site CCSite NameBusiness NameRanking
4812MEADOWLANDSTORONTO1
6867071ERINEDMONTON2
Revenue Total$10,813.82

<tbody>
</tbody>
 
Last edited:
Upvote 0
See if this fix it

Assumes data in Sheet SiteCopy columns A:D, headers in row 1, data beginning in row 2

Code:
Sub aTest()
    Dim lastRow As Long
    
    With Sheets("SiteCopy")
        lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        .Range("E1") = "Ranking"
        With Range("E2:E" & lastRow)
            .Formula = "=IF(D2=0,D2,RANK(D2,D$2:D$" & lastRow & "))"
            .Value = .Value
        End With
        .Range("A1:E" & lastRow).Sort key1:=.Range("D1"), order1:=xlDescending, Header:=xlYes
        .Range("E" & lastRow + 1) = .Range("D" & lastRow + 1)
        .Columns("D:D").Delete
    End With
            
End Sub

M.
 
Last edited:
Upvote 0
It's perfect! Thank you so much.

This is the new line of code that changed and what it was previously. I can see that in the new code it's counting up instead of down and refers to the column but not the row number this time. Although I can see the obvious change in the code I can't quite sort the logic out in my head to define what the new line of code does vs the previous line of code. Would you mind sharing with me an explanation so I can understand why it's now working.

lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'previous code: lastRow = .Cells(8, "C").End(xlDown).Row
 
Upvote 0
lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row

This is like going to the last cell in column C; hit the End key followed by the Up-arrow key.
It takes the cursor to the last row with data in column C.
Try it.

M.
 
Upvote 0
My previous codes (posts 6 and 7) aren't correct (my bad :oops:)
lastRow = .Cells(2, "B").End(xlDown).Row

It should be
lastRow = .Cells(1, "B").End(xlDown).Row

that would generated the same result (provided there aren't blank cells in middle of the data in column B)

But i don't know why your code in post 10 is
LastRow = .Cells(8, "C").End(xlDown).Row
From where came this 8?

M.
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,634
Members
449,460
Latest member
jgharbawi

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