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

[TABLE="width: 591"]
<tbody>[TR]
[TD]Country[/TD]
[TD]IATA Code[/TD]
[TD]Low[/TD]
[TD]High[/TD]
[TD][/TD]
[TD]Low[/TD]
[TD]High[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]121012[/TD]
[TD]121012[/TD]
[TD][/TD]
[TD]121012[/TD]
[TD]121013[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]121013[/TD]
[TD]121013[/TD]
[TD][/TD]
[TD]121101[/TD]
[TD]121101[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]121101[/TD]
[TD]121101[/TD]
[TD][/TD]
[TD]121103[/TD]
[TD]121103[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]121103[/TD]
[TD]121103[/TD]
[TD][/TD]
[TD]121105[/TD]
[TD]121107[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]121105[/TD]
[TD]121105[/TD]
[TD][/TD]
[TD]122052[/TD]
[TD]122052[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]121106[/TD]
[TD]121106[/TD]
[TD][/TD]
[TD]122104[/TD]
[TD]122104[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]121107[/TD]
[TD]121107[/TD]
[TD][/TD]
[TD]122108[/TD]
[TD]122108[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122052[/TD]
[TD]122052[/TD]
[TD][/TD]
[TD]122413[/TD]
[TD]122414[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122104[/TD]
[TD]122104[/TD]
[TD][/TD]
[TD]122502[/TD]
[TD]122506[/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122108[/TD]
[TD]122108[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122413[/TD]
[TD]122413[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122414[/TD]
[TD]122414[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122502[/TD]
[TD]122502[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122503[/TD]
[TD]122503[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122504[/TD]
[TD]122504[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122505[/TD]
[TD]122505[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]India[/TD]
[TD]IN[/TD]
[TD]122506[/TD]
[TD]122506[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Anyone with a solution to this?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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
 
Upvote 0
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!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top