Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Condensing Sequential Values to a Single Row.

Condensing Sequential Values to a Single Row

Written by Allen Wyatt (last updated September 14, 2019)
This tip applies to Excel 97, 2000, 2002, and 2003


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

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3853) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Condensing Sequential Values to a Single Row.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Specifying Index Section Dividers

When adding an index to your document, you can use one of the available field switches to specify how the index should be ...

Discover More

Displaying Latitude and Longitude

If you work with geographic data, you may need a way to display latitude and longitude in a worksheet. This tip examines ...

Discover More

Safely Relocking Forms

In order to use a form in Word, it must be protected. This means that you cannot make any changes to the form, even if ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Simulating Alt+Enter in a Formula

You can use the Alt+Enter keyboard shortcut while entering information in order to force your data onto multiple lines in ...

Discover More

Ignoring N/A Values in a Sum

You can use some of Excel's worksheet functions across a range or worksheets, but not all of them. One that has problems ...

Discover More

Calculating Statistical Values on Different-Sized Subsets of Data

Discovering different ways to analyze your data can be a challenge. Here's how to work with arbitrary subsets of a large ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.