# Condensing Sequential Values to a Single Row

Rusty has a list of ZIP Codes in a column of a worksheet. He would like a way to “compress” the codes so that sequential ranges of values are on a single row. So, for instance, instead of 35013, 35014, and 35015 taking up three rows, they would appear on a single row as 35013-35015.

There are a couple of ways to go about this-with or without macros. On the “without macros” side of the fence, there are a number of different approaches, and all of them involve the use of additional columns to hold intermediate results.

For example, let’s assume that you have your data in column A, starting in cell A2, and that cell A1 is empty (it doesn’t even have header text in it). In this case you could enter the following formula in cell B2:

```=IF(NOT(A2-A1=1),A2,IF(A3-A2=1,B1,A2))
```

Then, in cell C2, enter the following long formula:

```=IF(NOT(A3-A2=1),IF(A2-A1=1,TEXT(B1,"00000")
&" - "&TEXT(B2,"00000"),TEXT(A2,"00000")),"")
```

Now you can copy the formulas in cells B2:C2 down their respective columns. What you end up with in column C is the condensed series of ZIP Codes. You can copy these values-using Paste Special to ignore blank cells-to anyplace else you want.

If you want to use a macro approach, then there are no intermediate columns necessary. A macro can be written that essentially collapses the list of ZIP Codes in place. The following macro loops through whatever range of cells you selected and creates the condensed list:

```Sub CombineValues()
Dim rng As Range
Dim rCell As Range
Dim sNewArray() As String
Dim x As Long
Dim y As Long
Dim sStart As String
Dim sEnd As String

Set rng = Selection
sStart = rng.Cells(1)
sEnd = sStart
y = 1
For x = 1 To rng.Count - 1
If rng.Cells(x + 1) - _
rng.Cells(x) > 1 Then  'End
ReDim Preserve sNewArray(1 To y)
If sStart = sEnd Then
sNewArray(y) = sStart
Else
sNewArray(y) = sStart & "-" & sEnd
End If
sStart = rng.Cells(x + 1)
y = y + 1
End If
sEnd = rng.Cells(x + 1)
ReDim Preserve sNewArray(1 To y)
If sStart = sEnd Then
sNewArray(y) = sStart
Else
sNewArray(y) = sStart & "-" & sEnd
End If
Next
rng.ClearContents
For x = 1 To y
rng.Cells(x) = "'" & sNewArray(x)
Next
Set rng = Nothing
Set rCell = Nothing
End Sub
```