Create a low/high range of postal codes

kijake

New Member
Joined
Oct 9, 2019
Messages
2
Hi,

I have a list of postal codes arranged on 1 column of around 13000 rows.
Some of these postal codes could be collapsed and setup in ranges since they are consecutive like in the example below:

121012 and 121013 could be collapsed in 1 row as Low:121012 High:121013

CountryIATA CodeLowHighLowHigh
IndiaIN121012121012121012121013
IndiaIN121013121013121101121101
IndiaIN121101121101121103121103
IndiaIN121103121103121105121107
IndiaIN121105121105122052122052
IndiaIN121106121106122104122104
IndiaIN121107121107122108122108
IndiaIN122052122052122413122414
IndiaIN122104122104122502122506
IndiaIN122108122108
IndiaIN122413122413
IndiaIN122414122414
IndiaIN122502122502
IndiaIN122503122503
IndiaIN122504122504
IndiaIN122505122505
IndiaIN122506122506

<tbody>
</tbody>

Anyone with a solution to this?
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
35,509
Office Version
365
Platform
Windows
Hi & welcome to MrExcel.
How about
Rich (BB code):
Sub kijake()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long
   
   Ary = Range("C2", Range("C" & Rows.count).End(xlUp)).Value2
   ReDim Nary(1 To UBound(Ary), 1 To 2)
   Nary(1, 1) = Ary(1, 1)
   For r = 1 To UBound(Ary) - 1
      If Ary(r, 1) <> Ary(r + 1, 1) - 1 Then
         nr = nr + 1
         Nary(nr, 2) = Ary(r, 1)
         Nary(nr + 1, 1) = Ary(r + 1, 1)
      End If
   Next r
   Nary(nr + 1, 2) = Ary(r, 1)
   Range("F2").Resize(nr + 1, 2).Value = Nary
End Sub
 

kijake

New Member
Joined
Oct 9, 2019
Messages
2
Hi & welcome to MrExcel.
How about
Rich (BB code):
Sub kijake()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long
   
   Ary = Range("C2", Range("C" & Rows.count).End(xlUp)).Value2
   ReDim Nary(1 To UBound(Ary), 1 To 2)
   Nary(1, 1) = Ary(1, 1)
   For r = 1 To UBound(Ary) - 1
      If Ary(r, 1) <> Ary(r + 1, 1) - 1 Then
         nr = nr + 1
         Nary(nr, 2) = Ary(r, 1)
         Nary(nr + 1, 1) = Ary(r + 1, 1)
      End If
   Next r
   Nary(nr + 1, 2) = Ary(r, 1)
   Range("F2").Resize(nr + 1, 2).Value = Nary
End Sub

Thanks a lot, it worked!
In the meantime, I managed to find a much more manual solution.
- by calculating the difference between each postal code
- each occurrence where the difference is = 1 is define as the Low start of a range
- I then created a pivot table by setting my Low postal codes as rows, and my High postal codes as the data with field setting MAX

The result was the exact same as yours by much more time consuming. I'm not a VBA guy at all, so thanks again!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
35,509
Office Version
365
Platform
Windows
Glad to help & thanks for the feedback
 

Forum statistics

Threads
1,089,201
Messages
5,406,801
Members
403,106
Latest member
AliO

This Week's Hot Topics

Top