Convert Consecutive Excel PIN Codes into From and To Ranges
Question details
The user needs to group a large dataset of PIN codes into consecutive 'From' and 'To' range columns while preserving related row information such as dealer, branch, and state.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Organizing and consolidating extensive geographical or delivery routing data by converting lists of sequential PIN/zip codes into grouped range brackets.
- Observed behavior
- Individual PIN codes are currently listed row by row and need to be transformed so that consecutive sequences are summarized into start and end points, with isolated single PINs duplicated in both columns.
Make sure your data is correctly sorted numerically by the PIN code column and grouped by any relevant branch or dealer columns before attempting to transform the ranges.
Use Power Query to Group Consecutive PINs
Power Query provides a reliable, formula-free method to group sequential numbers by creating a custom grouping index.
This method is natively supported in Excel 365, Excel 2016, and Excel 2019. For Excel 2010 or 2013, the Power Query add-in must be downloaded and installed.
Select any cell in your data list, go to the 'Data' tab on the ribbon, and click 'From Table/Range' to open the Power Query Editor.
Click the drop-down arrow on your descriptive columns (like dealer or branch) to sort them, then sort the PIN code column in Ascending order.
Go to the 'Add Column' tab, click 'Index Column' (starting from 1). Then, add a Custom Column that subtracts the Index value from the PIN Code. This creates a unique identifier for each block of consecutive numbers.
Select your descriptive columns (dealer, state, etc.) along with the new Custom Column. Right-click and choose 'Group By'. Create two new aggregations: 'From' (using the Minimum of the PIN column) and 'To' (using the Maximum of the PIN column).
Remove any temporary index or custom identifier columns, then click 'Home' > 'Close & Load' to export the grouped ranges back into a new Excel sheet.

Use a VBA Macro for Older Excel Versions
If you are using an older version of Excel without Power Query, a VBA script can iterate through rows to identify and extract consecutive ranges.
Use WPS Spreadsheet to Organize PIN Code Ranges
WPS Spreadsheet provides powerful data processing tools. You can easily sort, filter, and use robust array formulas or VBA macros to group consecutive PIN codes without complex add-ins.
- 1. Open your dataset in WPS Spreadsheet: Launch WPS Office, open Spreadsheet, and load your original Excel file containing the PIN codes.
- 2. Sort and Add Helper Columns: Sort your data by branch and then by PIN. Use an IF formula (e.g., =IF(B2=B1+1, A1, B2)) to assign group IDs to consecutive sequences.
- 3. Extract Minimum and Maximum Values: Use the built-in PivotTable feature to group your data by the new ID, setting the PIN field to display both MIN (From) and MAX (To) values.

Frequently Asked Questions
Can I group consecutive numbers in Excel without Power Query?
Yes, you can use a combination of helper columns with IF formulas to identify gaps between numbers, and then use Pivot Tables or MINIFS/MAXIFS functions to extract the start and end of each range. Alternatively, VBA macros can automate this.
What happens if a PIN code does not have consecutive numbers next to it?
When calculating the minimum and maximum values for that specific grouping block, the single PIN code acts as both the start and end point. It will correctly be copied into both the 'From' and 'To' columns automatically.
Will grouping the PIN codes delete my other data columns like dealer and branch?
No. When using Power Query or Pivot Tables, you specify the descriptive columns (dealer, branch, state) as your primary grouping levels. The ranges are calculated strictly within those groups, keeping all related data perfectly intact.




