Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Reorganizing Data.

Reorganizing Data

by Allen Wyatt
(last updated April 4, 2016)

5

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8188) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Reorganizing Data.

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

Opening a Text File and Template from the Command Line

Word includes a command-line syntax that you can use to open files and do other operations. If you want to load a text file ...

Discover More

Browsing by Heading

The Object Browser can be a great (albeit underused) way of navigating through your document. One handy way to move about is ...

Discover More

Handling Negative Numbers in a Complex Custom Format

Custom formats are great for defining how a specific value in a cell should look. They aren't that great at doing complex ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Making Modal Dialog Boxes Appear in Front of Workbooks

Perhaps the most common way of communicating with programs is through the use of dialog boxes. We expect dialog boxes to be ...

Discover More

Forcing a Macro to Run when a Worksheet is Recalculated

Normally a macro is only calculated when you specifically tell Excel to calculate it. Some macros need to be calculated ...

Discover More

Deleting Zero Values from a Data Table

Want to get rid of all the zero values in a range of cells? This tip provides a couple of different ways you can accomplish ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 nine minus 5?

2016-04-06 11:22:18

Michael (Micky) Avidan

@To whom it may concern.
The same task can be achieved with a simple formula - as shown in the linked picture:
http://s14.postimg.org/jq7n3p6fl/NONAME.png
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2016-04-05 09:22:39

BananaTricky

This "worked" on a PowerPoint table copied from a PDF in that it sorted it into the requisite number of columns but it didn't take account of some columns having more than one entry in the original with the result that the results got skewed.

When I tried it on a more normal table of figures it crashed excel, getting stuck in a loop asking me how many columns I wanted.


2016-04-04 07:45:59

julie

I have tested this code and cannot get it to work for me - all it does is delete the info from the first cell of the column containing data.


2014-01-16 07:25:28

Jean-Baptiste Richard

Thanks for this hint. In addition, the "indirect()" function enables to do this without a macro by populating a table by "=indirect("A" & <whatever you need for calculating the number of the original line corresponding to your target cell>)". Then, if necessary, one can copy the values for getting rid of the initial column data.


2013-07-22 08:20:44

Bryan

It's a little confusing what's going on here and why you'd ever need to organize data in this way. A picture is worth 1000 words, and a concrete example is worth at least a couple hundred.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.