Written by Allen Wyatt (last updated August 5, 2021)
This tip applies to Excel 2007, 2010, and 2013
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:
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.
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!
You can create macros that are automatically executed whenever certain events occur within a worksheet. This tip details ...
Discover MoreSometimes you receive a phone number that contains alphabetic characters and you need to convert it to a purely numeric ...
Discover MoreDo you have a macro that needs to read and write files? If so, then there is a good chance you need to specify the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments