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

Written by Allen Wyatt (last updated August 5, 2021)
This tip applies to Excel 2007, 2010, and 2013


8

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 (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

Saving a Preview with Your Template

Templates provide a collection of styles and boilerplate for new documents. Selecting the right template by filename only ...

Discover More

X-Axis Dates Lose Formatting

Excel makes it easy to copy charts from one workbook to another. Even so, copying may produce some surprising results for ...

Discover More

Printing an Outline

Outlining is a great way to develop the content of your document. If you need to, you can even print the outline and only ...

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)

Finding Workbooks Containing Macros

Workbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might ...

Discover More

Debugging a Macro

Part of writing macros is to make sure they work as you expect. This involves a process known as debugging. Here's how ...

Discover More

Highlighting Pattern Violations

A common part of working with text strings in a worksheet is normalizing those strings so that they follow whatever rules ...

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 2 + 5?

2023-02-26 13:17:44

Willy Vanhaelen

Here is the macro you can copy:

Sub CompressData2()
Dim iTarget As Range
Dim J As Integer
Dim iTargetCols As Integer

iTargetCols = Val(InputBox("How many columns?"))
If iTargetCols < 2 Or Selection.Columns.Count > 1 Then Exit Sub

Set iTarget = Selection.Cells(1, 2).Resize(2, iTargetCols)

For J = 1 To Selection.Rows.Count
    iTarget.Cells(J) = Selection.Cells(J)
Next J
Selection.Delete
End Sub


2023-02-26 13:06:59

Wikke

The macro of this tip does a fine job but the definitions of the rSource and rTarget ranges are rather complex.

The rSource range needs even not to be defined because it is simply the Selection property.

While experimenting a bit I discovered that for the rTarget range it suffices to define a 2 rows range with the wanted number of columns. When VBA needs more rows, it simply adds them following the same pattern (number of columns). So, the definition could be reduced from 3 to 1 line:
Set iTarget = Selection.Cells(1, 2).Resize(2, iTargetCols).

The result is this smaller macro that does the same job:

(see Figure 1 below)

Figure 1. 


2023-02-18 15:16:36

Ron S

Macros are OK, but there are a few "new & improved" ways to do the same thing. And, some of them are much more wysiwyg than a macro.

The general concept that the idea of "reorganizing data" can be lumped under is "data cleaning".

A more specific search term is "unstack".

Unstack Data that is Separated by Bold Text: Challenge w/Kevin Lehrbass - Formulas, VBA, Power Query
https://www.youtube.com/watch?v=JsErvkFvYf8
Oz Du Soleil
Problem: a tall column of data that needs to be unstacked and converted into neat columns and rows.
Twist: the distinction where the list needs to be divided is by bold font.
(Download the workbook: http://datascopic.net/unstackingboldfont )
Parents' names are bold.
Childrens' names are regular font.
How does this list get unstacked with the children next to the right parent?
I open with Excel's Get and Transform (Power Query).
a formula-based solution and
a VBA solution!

Dynamic Array: Unstack Data with SEQUENCE ... just 2 steps
https://www.youtube.com/watch?v=ODOSw1-w5Ws
Oz Du Soleil
I was playing around more with the Dynamic Array functions, especially after Leila Gharani posted her video that made unstacking data pretty easy with a substitution trick and finding a pattern in the dataset. Here is her video: https://www.youtube.com/watch?v=rscXN...
Well, I found a 2-step process that REALLY simplifies unstacking a column of data. SEQUENCE, COUNTA and INDEX get us there
Alas, not everyone has the Dynamic Arrays yet. If you don't have them, and you need to unstack some data, use Leila's method.

Excel Dynamic Arrays: Unstack a Column of Records
https://www.youtube.com/watch?v=dy1DwE-B9ug
Oz Du Soleil
The classic problem: unstack a column of records in Excel. This video shows a cool way to get this stuff un-stacked. You'll see:
. * the FILTER Dynamic Array function,
. * the SEQUENCE Dynamic Array Function, and even
. * the SORTBY.

How to convert Stack Data into Unstack different approaches | Unstack Uneven Data Excel Power Query
https://www.youtube.com/watch?v=X6XTTxjAg14
. * using tables and formula,
. * PowerQuery
. * the new Dynamic Array formula functions

Note: many of these ideas are limited to Excel 2021 or 365 because they use new features and functions
.
Move Data from One Column into Multiple Columns in Excel 365 (Array Formula)
https://excelmee.com/excel-formulas/move-data-from-one-column-into-multiple-columns-excel-365/
Step 1 (Lookup Value)
Step 2 (Lookup Array)
Step 3 (Return Array)
Final Step (XLOOKUP Formula to Unstack Data)


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.