How to rank values by group in Excel?
Working with grouped data in Excel often requires comparing values within each group, such as ranking sales figures by region, test scores by class, or transaction amounts by category. While Excel provides robust tools for ranking data, ranking within groups (also known as "group-wise ranking" or "conditional ranking") requires a specific approach. This is especially useful when you need to evaluate performance or identify top and bottom records among different categories without mixing results between groups. The following methods explore practical solutions for ranking values by group, making it easier to interpret and analyze your data accurately in everyday tasks.
Rank values by group
VBA Code - Use a macro to automate ranking values within each group
Rank values by group
For situations where you need to rank values within distinct groups, such as scoring students by class or listing sales by different regions, Excel does not have a direct "rank by group" feature. However, a well-crafted formula can achieve group-wise ranking efficiently and without extra data manipulation.
To do this, you can use an array formula that combines logical tests with aggregation functions. This approach allows you to compare each value only within its designated group, producing the required rank for each data point.
Follow these steps:
- Prepare your grouped data in columns, such as Group (A2:A11) and Value (B2:B11).
- Select a blank cell adjacent to your data—usually, in the first row next to your values, for example, cell C2.
- Enter the following formula:
=SUMPRODUCT(($A$2:$A$11=A2)*(B2<$B$2:$B$11))+1
This formula works by counting how many values within the same group are less than the current value. Here’s what each parameter means:
- ($A$2:$A$11=A2)
→ This checks if each cell in the range A2:A11 equals the value in A2.
→ It returns an array of TRUE/FALSE (or 1/0) values representing whether each row belongs to the same group as A2. - (B2<$B$2:$B$11)
→ This checks how many values in B2:B11 are greater than B2.
→ It returns TRUE (1) if B2 is less than a given value, FALSE (0) otherwise. - * (Multiplication)
→ This combines the two conditions: - Group match (A2)
Value in B2 is smaller than others
→ So only rows that are in the same group and have a smaller value will count. - SUMPRODUCT(...)
→ Sums up the number of rows that meet both conditions. - +1
→ Ranks start at 1 (instead of 0), so we add 1 to the count of smaller values.
Once the formula is entered in C2, drag the autofill handle down to fill this formula for all relevant rows in your dataset. The formula will automatically adjust to use each row's group and value, returning the rank within that group.
Tips and precautions:
- If your range is large, remember to update the cell references accordingly.
- For ranks in descending order (e.g., highest value is1), modify the formula comparison from
B2<$B$2:$B$11
toB2>$B$2:$B$11
. - To handle duplicate values, this formula assigns the same rank to equal values within the same group. If you require sequential unique ranks, consider using additional helper columns.
This formula-based method is flexible and easily applies to most grouped table structures in Excel. However, for very large datasets, calculation performance may slow down due to reliance on array logic.
VBA Code - Use a macro to automate ranking values within each group
For users looking to automate the ranking process, or handle larger datasets more efficiently, writing a VBA macro can be a valuable approach. Macros can automate repetitive steps, provide more customization, and process data quickly compared to complex formulas. This is ideal for scenarios like scheduled report generation, repeated ranking tasks, or when you want to avoid formula clutter in your worksheet.
Before proceeding, be sure to save your work and enable macros in your Excel settings. Here’s how you can script and run this solution:
- Press Alt + F11 keys to enable the VBA editor. In the Microsoft Visual Basic for Applications window that appears, click Insert > Module, then paste the following code into the opened module:
Sub RankValuesByGroup()
Dim DataRange As Range
Dim GroupRng As Range
Dim ValueRng As Range
Dim OutCol As Range
Dim dictGroups As Object
Dim arrValues, arrRanks
Dim i As Long, j As Long
Dim GroupKey As String
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set DataRange = Application.InputBox("Select the data table range (including group and value columns)", xTitleId, Selection.Address, Type:=8)
If DataRange Is Nothing Then Exit Sub
Set GroupRng = Application.InputBox("Select the group column within your range", xTitleId, DataRange.Columns(1).Address, Type:=8)
Set ValueRng = Application.InputBox("Select the value column to rank within your range", xTitleId, DataRange.Columns(2).Address, Type:=8)
Set OutCol = DataRange.Offset(0, DataRange.Columns.Count).Resize(DataRange.Rows.Count, 1)
OutCol.Cells(1).Value = "RankByGroup"
Set dictGroups = CreateObject("Scripting.Dictionary")
arrValues = ValueRng.Value
arrRanks = ValueRng.Value
' Build group dictionaries for ranking
For i = 2 To UBound(arrValues, 1)
GroupKey = GroupRng.Cells(i, 1).Value
If Not dictGroups.Exists(GroupKey) Then
dictGroups.Add GroupKey, CreateObject("System.Collections.ArrayList")
End If
dictGroups(GroupKey).Add arrValues(i, 1)
Next i
' Rank within each group
For i = 2 To UBound(arrValues, 1)
GroupKey = GroupRng.Cells(i, 1).Value
Dim countLower As Long
countLower = 0
For j = 0 To dictGroups(GroupKey).Count - 1
If dictGroups(GroupKey)(j) < arrValues(i, 1) Then
countLower = countLower + 1
End If
Next j
arrRanks(i, 1) = countLower + 1
Next i
' Output results
For i = 2 To UBound(arrRanks, 1)
OutCol.Cells(i, 1).Value = arrRanks(i, 1)
Next i
MsgBox "Ranking by group completed.", vbInformation, xTitleId
End Sub
- Click Run. A dialog box will prompt you to select your full data range, group column, and value column. The macro will then generate a new column with the ranks for each value within its group.
Notes and troubleshooting:
- Ensure column selections match your data: the group and value columns must align correctly.
- If the data header is included, adjust the initial loop index in code for correct ranking (as per your data structure).
- To rank in descending order, modify the comparison
If dictGroups(GroupKey)(j) < arrValues(i,1)
accordingly. - If you encounter permission or macro security warnings, check Excel’s Macro Security settings under File > Options > Trust Center.
This VBA method offers flexibility and robust performance for more advanced or scaled-up applications, especially when integrated with automated reporting workflows.

Unlock Excel Magic with Kutools AI
- Smart Execution: Perform cell operations, analyze data, and create charts—all driven by simple commands.
- Custom Formulas: Generate tailored formulas to streamline your workflows.
- VBA Coding: Write and implement VBA code effortlessly.
- Formula Interpretation: Understand complex formulas with ease.
- Text Translation: Break language barriers within your spreadsheets.
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!