Reorganizing Data

Written by Allen Wyatt (last updated February 18, 2023)
This tip applies to Excel 97, 2000, 2002, and 2003


If you import a data list into Excel, it is not unusual to end up with a lot of data in column A. In fact, it is not unusual to have nothing in any of the other columns. (This all depends on the nature of the data you are importing, of course.) As part of working with the data in Excel, you may want to "reorganize" the data so that it is pulled up into more columns than just column A.

As an example, imagine that you imported your data, and it ended up occupying rows 1 through 212 of column A. What you really want is for the data to occupy columns A through F, of however many rows are necessary to hold the data. Thus, A2 needs to be moved to B1, A3 to C1, A4, to D1, A5 to E1, A6 to F1, and then A7 to A2, A8 to B2, etc.

To reorganize data in this manner, you can use the following macro. Select the data you want to reorganize, and then run the macro. You are asked how many columns you want in the reorganized data, and then the data shifting begins.

Sub CompressData()
    Dim rSource As Range
    Dim rTarget As Range
    Dim iWriteRow As Integer
    Dim iWriteCol As Integer
    Dim iColCount As Integer
    Dim iTargetCols As Integer
    Dim J As Integer

    iTargetCols = Val(InputBox("How many columns?"))
    If iTargetCols > 1 Then
        Set rSource = ActiveSheet.Range(ActiveWindow.Selection.Address)
        If rSource.Columns.Count > 1 Then Exit Sub

        iWriteRow = rSource.Row + (rSource.Cells.Count / iTargetCols)
        iWriteCol = rSource.Column + iTargetCols - 1
        Set rTarget = Range(Cells(rSource.Row, rSource.Column), _
          Cells(iWriteRow, iWriteCol))

        For J = 1 To rSource.Cells.Count
            rTarget.Cells(J) = rSource.Cells(J)
            If J > (rSource.Cells.Count / iTargetCols) Then _
              rSource.Cells(J).Clear
        Next J
    End If
End Sub

The macro transfers information by defining two ranges: the source range you selected when you ran the macro and the target range defined by the calculated size based on the number of columns you want. The source range is represented by the rSource variable object, and the target range by rTarget. The For ... Next loop is used to actually transfer the values.

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 (2301) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Changing Chart Size

Place a chart on a worksheet and you may not be satisfied with its size. Changing the size of a chart is a simple process ...

Discover More

Stopping Row Breaking for Many Tables

Got a lot of tables you need to format all at once? While you could make your changes manually, a much quicker way is to ...

Discover More

Calculating the First Tuesday

Do you need to figure out the date for the first Tuesday of any given month? Excel is incredibly flexible when it comes ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Counting All Characters

Need to know how many characters there are in a workbook? You can find out easily with the handy macro introduced in this ...

Discover More

Running a Macro when a Workbook is Closed

One of the automatic macros you can set up in Excel is one that is triggered when a workbook is closed. This tip explains ...

Discover More

Converting from Relative to Absolute

Addresses used in a formula can be either relative or absolute. If you need to switch between the two types of ...

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 one less than 9?

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.