VBA & Macro Problems

How to Replace Excel Group Names with 'Other' When Count is Below 10

Olivia MillerOlivia Miller Oct 26, 2026 868 views

Question details

The user needs to consolidate low-frequency categorical data by renaming any group that appears less than 10 times in a column to 'Other'.

How to Replace Excel Group Names with 'Other' When Count is Below 10
Product
Excel
Device & OS
not provided
Scenario
Cleaning and organizing datasets where minor, infrequent group names (like specific colors) need to be combined into a general 'Other' category for easier analysis.
Observed behavior
The column currently contains multiple individual group names, and manual replacement is inefficient for large datasets.
Before you start

Ensure your target data begins in cell D2 of the active worksheet and verify there are no blank cells within the column, as this may interrupt the data range selection.

Solution 1Recommended

Use a VBA Macro to Replace Low-Frequency Group Names

Execute a custom VBA script to quickly evaluate the column array in memory and overwrite groups appearing fewer than 10 times with 'Other'.

Using VBA is the most efficient method for this task because it processes the data array in the background (disabling screen updating) and overwrites the original cells instantly.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to launch the Microsoft Visual Basic for Applications window.

2
Insert a New Module

Click 'Insert' in the top menu and select 'Module' to create a blank workspace for your code.

3
Paste the Macro Code

Copy and paste the following code into the module window: Sub Set2Other() Dim rng As Range Dim v As Variant Dim i As Long Set rng = Range(Range("D2"), Range("D2").End(xlDown)) v = rng.Value For i = 1 To UBound(v) If Application.CountIf(rng, v(i, 1)) < 10 Then v(i, 1) = "Other" End If Next i Application.ScreenUpdating = False rng.Value = v Application.ScreenUpdating = True End Sub

4
Run the Macro

Press F5 or click the 'Run' button (the green triangle) in the toolbar. The data in column D will be automatically updated.

Use a VBA Macro to Replace Low-Frequency Group Names
Data Range Adjustment: If your data starts in a different column or row (e.g., A2), ensure you change both instances of 'Range("D2")' in the script to match your starting cell.
Advanced Data Processing

Easily Manage Macros and Formulas with WPS Spreadsheet

WPS Office offers full support for complex data processing, including advanced formulas and VBA macros, allowing you to quickly clean and consolidate your data without hassle.

  1. 1. Open your file in WPS Spreadsheet: Launch WPS Office and open your workbook containing the categorized data.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon to access the VBA Editor and macro settings.
  3. 3. Execute the Macro or Formula: Insert the VBA code or helper column formulas just as you would in Microsoft Excel to instantly update your group categories.
Fully compatible with Microsoft Excel formulas and VBA macros.Lightweight software that processes large datasets with high performance.Intuitive developer interface for easy code management and debugging.
microsoft office alternative - wps office

Frequently Asked Questions

How can I change the minimum count threshold from 10 to another number?

In the VBA code, locate the line `If Application.CountIf(rng, v(i, 1)) < 10 Then` and change the number 10 to your desired threshold (e.g., 5 or 20). If using the formula method, change the `<10` in the formula accordingly.

Why is the macro not updating all of my data?

The provided VBA macro uses `End(xlDown)` to define the range. If there are blank cells within your data column, the macro will stop at the first blank cell. Ensure your dataset is contiguous or manually define the range in the script.

Can I apply this macro to multiple columns at once?

The current script is designed for a single specific column (D). To apply it to multiple columns, you would need to modify the VBA script to loop through each column or adjust the range definition to encompass a multi-column array.

How do I save a workbook that contains VBA macros?

To preserve your VBA code, you must save your file as an Excel Macro-Enabled Workbook (.xlsm). Go to File > Save As, and select 'Excel Macro-Enabled Workbook' from the file format dropdown.