VBA & Macro Problems

How to Group Consecutive ZIP+4 Numbers into Ranges Using Excel VBA

Huda QurayshiHuda Qurayshi Nov 3, 2026 868 views

Question details

Consolidate large datasets of sequential nine-digit ZIP+4 numbers into start and end ranges on a separate worksheet.

How to Group Consecutive ZIP+4 Numbers into Ranges Using Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Managing and organizing large datasets of ZIP+4 codes by grouping consecutive numerical values together to save space and display distinct ranges.
Observed behavior
Sequential nine-digit numbers are listed individually in rows and need to be consolidated automatically into start and end ranges on a worksheet named 'Ranges'.
Before you start

Ensure your ZIP+4 numbers are sorted in ascending order starting from row 2, remove any duplicate values, and create a blank worksheet named 'Ranges' to store the extracted output.

Solution 1Recommended

Use a VBA Macro to Group Consecutive Values

A VBA macro automates the process of reading your sorted data, detecting sequential numbers, and outputting the start and end values for each range.

This method is highly efficient for large datasets. It iterates through the sorted ZIP+4 values in column A, uses a Collection or array to track each consecutive sequence, and automatically writes the starting and ending numbers to the 'Ranges' worksheet.

1
Sort the Data

Select your ZIP+4 values in Column A and sort them in ascending order using the Sort feature on the Data tab.

2
Launch the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor, then navigate to Insert > Module to create a new script window.

3
Input the Grouping Code

Write a VBA script that loops through the rows in Column A, evaluating if the current cell equals the previous cell plus one. Instruct the macro to record the boundaries of each sequence.

4
Output to the Ranges Sheet

Set the output destination in your VBA code to Worksheets("Ranges").Range("A2"). Press F5 or click Run to execute the script and generate your start and end columns.

Use a VBA Macro to Group Consecutive Values
Performance Tip: For massive datasets, reading the range into a VBA array before processing will significantly speed up the macro compared to evaluating cell by cell.
WPS Spreadsheet VBA Support

Run Grouping Macros Seamlessly in WPS Spreadsheet

WPS Office offers comprehensive support for VBA macros, allowing you to run your ZIP+4 grouping scripts just like you would in Microsoft Excel, ensuring highly efficient data processing in a lightweight application.

  1. 1. Open Your Dataset: Launch WPS Office and open the workbook containing your sequential ZIP+4 numbers.
  2. 2. Access the Macro Editor: Navigate to the Tools tab on the ribbon and click on 'Macro' to open the Visual Basic Editor.
  3. 3. Execute the Grouping Script: Insert a new module, paste your VBA code for grouping sequences, and click Run to automatically generate your ranges.
Fully compatible with Microsoft Excel VBA scripts and modulesNative support for .xlsm, .xlsb, and .xlsx file formatsFamiliar macro editor interface with zero learning curveHigh performance for handling large datasets quicklyFree and robust alternative for advanced data management
microsoft office alternative - wps office

Frequently Asked Questions

Why do my nine-digit ZIP+4 numbers lose their leading zeros in Excel?

By default, Excel treats numeric entries as standard numbers and strips out leading zeros. To preserve them, format the column as Text or apply a Custom Number Format (e.g., 000000000) before typing or importing your ZIP+4 values.

Can the VBA macro group ZIP+4 values if they are formatted as text?

If the values are strictly formatted as text, standard mathematical operations (like checking if A2 = A1 + 1) may fail. You need to convert the text strings to numerical values within your VBA code using functions like CLng() or CDbl() before evaluating the sequences.

What happens if there are duplicate ZIP+4 values in my dataset?

Duplicate values will disrupt the consecutive number logic, as a duplicate evaluates to the same number (not +1). Always use the 'Remove Duplicates' feature on the Data tab to clean your list before running the VBA macro or Power Query.