Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Reorganizing Data.
by Allen Wyatt
(last updated April 19, 2014)
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 (2301) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Reorganizing Data.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Macros are great for working with strings, and one of the most commonly used string functions is Len. This tip explains ...Discover More
When reading information from a text file, your macro may need to start reading at a place other than the beginning of ...Discover More
You can hide a bunch of worksheets at the same time, but Excel makes it impossible to unhide a bunch at once. You can, ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.