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?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,406
Office Version
  1. 365
Platform
  1. 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
56,406
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,129,269
Messages
5,635,195
Members
416,846
Latest member
ImGoing2needaFormula

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
Top