This article was previously published under Q247311
This article has been archived. It is offered "as is" and will no longer be updated.
Microsoft Excel does not have a built-in method to sort a newspaper (snaking) column of data. This article contains a sample macro to sort data that is in a newspaper column format.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
In Microsoft Excel, you usually use the sorting feature to sort a list. A list in Excel is a series of worksheet rows that contain related data, such as an invoice database or a set of client names and phone numbers. You can use a list as a database, in which each row is a record, and the columns are fields. The first row of the list usually has labels for the columns, for example:
Newspaper columns and snaking columns are different terms for the same type of data format. In newspaper columns, the data fills one column and continues at the top of the next column. In this case, your data probably does not have labels for each column, for example:
On the Tools menu, point to Macro, and then click Visual Basic Editor.
On the Insert menu, click Module.
Type the following code in the module:
Note The following example demonstrates how to sort in ascending order. You can change this by modifying the value of the "Order1:=" argument to xlDescending.
Sub SortAllRangeData() ' Place column header for temporary sort area. Range("IV1").Value = "Numbers" ' Move numbers to temporary sort location. For Each cell In Selection Range("iv65536").End(xlUp).Offset(1, 0) = cell.Value Next cell ' Sort numbers in ascending order. Range("IV1", Range("IV1").End(xlDown)).Sort Key1:=Range("IV2"), _ Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom ' Move sorted data back to original sheet location. Selection(1, 1).Activate ' Make sure the ActiveCell is the ' top left of Selection first. CCnt = Selection.Columns.Count RCnt = Selection.Rows.Count CellCnt = Selection.Cells.Count Tcell = 2 For c = 1 To CCnt For r = 1 To RCnt Range(ActiveCell.Address).Offset(r - 1, c - 1).Value = _ Range("iv" & Tcell).Value Tcell = Tcell + 1 Next r Next c ' Clean up temporary sort location. Range("IV1", Range("IV1").End(xlDown)).ClearEnd Sub
On the Excel menu in Microsoft Excel X for Mac and in later versions of Excel for Mac, or on the File menu in all other versions of Excel, click Close and Return to Microsoft Excel.
On the Tools menu, point to Macro, and then click Macros. Select the SortAllRangeData macro, and then click Run.
All the data within the selected range is now sorted, and appears as follows: