Rank based on multiple columns

jrudner

Board Regular
Joined
Jul 16, 2008
Messages
100
Hello, I have three columns in a datasheet as follows:

Column A: Disease
Column B: Hospital
Column C: Grams_Administered
Column D: Rank

Basically , I need to rank each row (disease) respectively within its hospital based on the Grams_Administered. In the rank column, therefore, there will be multiple entries with the same rank because there are multiple hospitals. Any ideas?

Thanks,

J
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi J,

Just to clarify, you need to know the totals for each hospital, which diseases have the most grams?

I don't think you can specify criteria in rank, so I suggest creating a separate table PER HOSPITAL, then adding a list of the diseases in the same column. Then use SUMPRODUCT to calculate the totals, then add a rank column and hey presto, not ideal but it will be difficult to do this any other way I think.

Something like...

ColA...............ColB..................................ColC.....
Disease..........Hospital A...........................Rank
DisA..............=sumproduct((etc.etc.....))....=rank(etc.etc.

Ian
 
Upvote 0
Do you know of a formula that I could use? I did this exact thing once using the * (asterisk) operator and it worked like a charm. Sadly I can't recall the formula :(
 
Upvote 0
The only approach that I can think of is a series of helper columns - two for each hospital - one for the value, and the other for the rank. You will then need an additional pair of columns to give you the rank overall. you can avoid repeated rankings by using :-

RANK(A1,A1:A100) + COUNTIF($A1:A1) - 1 in the overall rank column

Thanks

Kaps
 
Upvote 0
Found it!

=SUMPRODUCT(($B$2:$B$1030=B2)*(C2<$C$2:$C$1030))+1

The name of the disease doesn't matter, just the total grams of it within the hospital.
 
Upvote 0
Hi, Although you seem to have a solution, you could try this.
Data based on your first post.
Results by Hospital then Greatest Grams per disease.
Results in cell "F2" on.
Nb:- This is all one code !
Code:
Dim rCells As Range, rCell As Range, oHosp As Variant, c, nCell As String
Dim oDis As Variant, oH As Integer, oD As Integer
Set rCells = Range("B2", Range("B" & Rows.Count).End(xlUp))
 Dim a
 
 With CreateObject("scripting.dictionary")
    For Each rCell In rCells
                If rCell <> "" Then
            If Not .Exists(rCell.Value) Then
                .Add rCell.Value, rCells
                c = c + 1
             End If
        End If
    Next rCell
    oHosp = .keys
End With
'----------
Set rCells = Range("a2", Range("a" & Rows.Count).End(xlUp))

c = 0
 
 With CreateObject("scripting.dictionary")
    For Each rCell In rCells
                If rCell <> "" Then
            If Not .Exists(rCell.Value) Then
                .Add rCell.Value, rCells
                c = c + 1
             End If
        End If
    Next rCell
    oDis = .keys
End With
 

'==================

Dim sortArray, i As Long, j As Long, temp(1 To 4) As Variant
Dim Rng As Range
ReDim sortArray(1 To UBound(oHosp) + 1, 1 To 4)

For oH = 0 To UBound(oHosp)
c = 0
For oD = 0 To UBound(oDis)
For Each rCell In rCells

If rCell.Offset(, 1) = oHosp(oH) And rCell = oDis(oD) Then
c = c + 1

sortArray(c, 1) = rCell.Offset(, 1)
    sortArray(c, 2) = rCell
        sortArray(c, 3) = rCell.Offset(, 2)
        sortArray(c, 4) = c 
End If
Next rCell
Next oD

Dim Last As Integer


For i = 1 To (UBound(sortArray) - 1)
   For j = i To UBound(sortArray)

      If sortArray(j, 3) > sortArray(i, 3) Then
         
         temp(1) = sortArray(i, 1)
            temp(2) = sortArray(i, 2)
                temp(3) = sortArray(i, 3)
         
         sortArray(i, 1) = sortArray(j, 1)
            sortArray(i, 2) = sortArray(j, 2)
                sortArray(i, 3) = sortArray(j, 3)
        
         sortArray(j, 1) = temp(1)
            sortArray(j, 2) = temp(2)
                sortArray(j, 3) = temp(3)
     
      End If
   Next j
Next i
Last = Range("f" & Rows.Count).End(xlUp).Row + 1
Range("f" & Last).Resize(c, 4).Value = sortArray

Next oH
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,140
Members
448,551
Latest member
Sienna de Souza

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