VBA & Macro Problems

Group Names by Score Range in Excel with VBA

Bushra ParveenBushra Parveen Nov 14, 2026 868 views

Question details

Create an Excel solution that places names under score-range headings and keeps a separate output row for each category.

Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet contains names and scores that need to be grouped into separate columns by score range.
Observed behavior
Each name should appear in the next available row under the heading for its score range.
Before you start

Arrange names in column A and scores in column B, with headers in row 1. Reserve columns D through G for the results and check that they contain no data you need, because the macro clears those columns below the headers.

Solution 1Recommended

Use VBA to group names into score-range columns

Use a macro when you want to process the score list and maintain a separate output-row counter for each range.

This example groups scores into below 30, 30 to below 50, 50 to below 70, and 70 or above. Each category starts at row 2 in its own output column.

Blank scores, nonnumeric scores, errors, and rows without a name are skipped. Change the Select Case limits and output headings if your ranges differ.

1
Open the VBA editor

In desktop Excel, open the workbook and press Alt+F11, or select Developer > Visual Basic. In the VBA editor, choose Insert > Module.

2
Add the macro

Paste this code into the module: Sub GroupNamesByScore() Dim ws As Worksheet Dim lastRow As Long, r As Long, category As Long Dim nextRow(1 To 4) As Long Dim score As Double, i As Long Set ws = ActiveSheet lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ws.Range("D2:G" & ws.Rows.Count).ClearContents ws.Range("D1").Value = "Below 30" ws.Range("E1").Value = "30 to below 50" ws.Range("F1").Value = "50 to below 70" ws.Range("G1").Value = "70 or above" For i = 1 To 4 nextRow(i) = 2 Next i For r = 2 To lastRow If Not IsError(ws.Cells(r, "A").Value) And Not IsError(ws.Cells(r, "B").Value) Then If Len(Trim(CStr(ws.Cells(r, "A").Value))) > 0 And Len(Trim(CStr(ws.Cells(r, "B").Value))) > 0 Then If IsNumeric(ws.Cells(r, "B").Value) Then score = CDbl(ws.Cells(r, "B").Value) Select Case score Case Is < 30: category = 1 Case Is < 50: category = 2 Case Is < 70: category = 3 Case Else: category = 4 End Select ws.Cells(nextRow(category), category + 3).Value = ws.Cells(r, "A").Value nextRow(category) = nextRow(category) + 1 End If End If End If Next r End Sub

3
Save and run the macro

Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Return to the worksheet you want to process, choose Developer > Macros, select GroupNamesByScore, and click Run.

4
Check the results

Review columns D through G. Each name should appear under its score-range heading, with no gaps caused by names assigned to other categories.

Use VBA to group names into score-range columns
Running the macro again: The macro clears the existing contents of D2:G on the active worksheet before writing new results. It also replaces the headings in D1:G1.
Group scores in WPS Spreadsheets

Group names by score range in WPS Spreadsheets

Use filters in WPS Spreadsheets to show each score range and copy its names into a separate output column, without writing a macro.

  1. 1. Open the score list: Open your Excel file in WPS Spreadsheets. Put names in column A and numeric scores in column B, with headers in row 1.
  2. 2. Set up output headings: Enter your score-range headings in empty columns, such as D1:G1, and leave the cells below them empty.
  3. 3. Filter the first range: Select the source table and choose Data > Filter. Open the score column's filter menu and set a number filter for the first range, such as scores below 30.
  4. 4. Copy and repeat: Select and copy the visible names below the name header, then paste them below the corresponding output heading. Change the score filter for each remaining range and repeat; clear the filter when finished.
Group names into score categories using spreadsheet filtersOpen and edit Microsoft Excel XLSX and XLS filesUse a familiar spreadsheet interfaceGet a free, lightweight Office suite
QA img-9

Frequently Asked Questions

How do the VBA counters prevent gaps in each category?

The macro initializes one nextRow counter per category at row 2. After writing a name to that category's column, it increments only that category's counter.

Where does a score of exactly 30 go?

A score of 30 goes into the 30 to below 50 category. Similarly, 50 goes into the next category and 70 goes into the 70 or above category.

Why does my FILTER formula show a spill error?

Check the cells below the formula for existing content and clear anything blocking the results. Each FILTER formula needs an empty spill area in its output column.

Can I change the score ranges?

Yes. In the VBA editor, change the limits in the Select Case statements and update the D1:G1 headings. For the formula approach, change the comparison limits in the FILTER formulas and their headings.

Why does a numeric-looking score appear in the macro results but not the FILTER results?

The macro accepts numeric text through IsNumeric, while the FILTER formulas use ISNUMBER and require actual numeric values. Convert text scores to numbers in column B for consistent results.