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

Changing the Size of a Graphic

Word allows you to add more than text to your documents; you can also add graphics. Once added, you can modify the size of ...

Discover More

Updating Calculated Fields in a Form

When creating a Word form, you use special form fields to collect information from users. You can even perform calculations ...

Discover More

ExcelTips Ribbon 2012 Archive (Table of Contents)

ExcelTips is a weekly newsletter that provides tips on how to best use Microsoft's word processing software. At the ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Saving Changes when Closing

If your macro closes workbooks, you'll want to make sure that it will save any changes you made to the workbook. Here's how ...

Discover More

Removing Pictures for a Worksheet in VBA

Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time figuring ...

Discover More

Resizing Checkboxes

If you create a user form in VBA that includes checkboxes, you may want to make the checkboxes larger. You can't adjust their ...

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. Maximum image size is 8Mpixels. 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 6 - 3?

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.