View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Duke Carey
 
Posts: n/a
Default Need to convert list of 5 digit zip codes to ranges where possible

As alternative to Bruce's suggestion, here's a macro that might do the trick

Sub zips()
Dim rng As Range
Dim strFirst As String
Dim intCurr As Long
Dim strlast As String
Dim rngTgt As Range
Dim x As Integer
Dim lTest As Long

Range("B1:B100").ClearContents

Set rngTgt = Range("B1")
strFirst = ""
strlast = ""
intCurr = 0
For Each rng In Selection
lTest = CLng(rng)
If (lTest intCurr + 1) Then

' no sequence, so write what you have
If Len(strlast) = 0 Then
rngTgt.Offset(x, 0) = strFirst
x = x + 1
Else
rngTgt.Offset(x, 0) = strFirst & " - " & strlast
x = x + 1
End If
strFirst = rng
intCurr = CLng(strFirst)
strlast = ""
Else
' it's still a sequence so increase the upper end and
' the comparison value
strlast = rng
intCurr = CLng(rng)
End If
Next

End Sub

For information on installing the code see
Getting Started with Macros and User Defined Functions

http://www.mvps.org/dmcritchie/excel/getstarted.htm



"Mel07" wrote:


I have been unable to figure this one out. I have over 2000 zip codes
in a single column (One 5 digit zip code per row) which are already
sorted in numeric order. I need to put these individucal zip codes
into ranges where possible when zip codes are sequential (i.e.
91714-91734). Is this possible outside of doing this manually because
it's killing me!!! Help.


--
Mel07
------------------------------------------------------------------------
Mel07's Profile: http://www.excelforum.com/member.php...o&userid=32458
View this thread: http://www.excelforum.com/showthread...hreadid=522385