Transform range of cells to one column or row

mswoods1

Board Regular
Joined
Aug 6, 2010
Messages
60
This is something I needed to do today and wondered if anyone had a more elegant solution:

I had a range of cells, which were in the shape of a box, like this:<style type="text/css">
table.tableizer-table {border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px;} .tableizer-table td {padding: 4px; margin: 3px; border: 1px solid #ccc;}
.tableizer-table th {background-color: #104E8B; color: #FFF; font-weight: bold;}
</style>
<table class="tableizer-table">
<tr><td>d</td><td>e</td><td>c</td><td>d</td></tr> <tr><td>i</td><td>f</td><td>g</td><td>b</td></tr> <tr><td>a</td><td>j</td><td>k</td><td>h</td></tr></table>
I needed to transform the cells into a column and sort them to look like this:<style type="text/css">
table.tableizer-table {border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px;} .tableizer-table td {padding: 4px; margin: 3px; border: 1px solid #ccc;}
.tableizer-table th {background-color: #104E8B; color: #FFF; font-weight: bold;}
</style>
<table class="tableizer-table">
<tr class="tableizer-firstrow"><th></th></tr> <tr><td>a</td></tr> <tr><td>b</td></tr> <tr><td>c</td></tr> <tr><td>d</td></tr> <tr><td>e</td></tr> <tr><td>f</td></tr> <tr><td>g</td></tr> <tr><td>h</td></tr> <tr><td>i</td></tr></table>
The way I did it was to first transform the range into a column by using the INDEX function and manually putting in the row/number that I needed, like this: <style type="text/css">
table.tableizer-table {border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px;} .tableizer-table td {padding: 4px; margin: 3px; border: 1px solid #ccc;}
.tableizer-table th {background-color: #104E8B; color: #FFF; font-weight: bold;}
</style>

<table class="tableizer-table">
<tr class="tableizer-firstrow"><th>row</th><th>column</th><th>letter</th><th>formula</th></tr> <tr><td>1</td><td>1</td><td>d</td><td>=INDEX($A$1:$D$3,A6,B6)</td></tr> <tr><td>1</td><td>2</td><td>e</td><td>=INDEX($A$1:$D$3,A7,B7)</td></tr> <tr><td>1</td><td>3</td><td>c</td><td>=INDEX($A$1:$D$3,A8,B8)</td></tr> <tr><td>1</td><td>4</td><td>d</td><td>=INDEX($A$1:$D$3,A9,B9)</td></tr> <tr><td>2</td><td>1</td><td>i</td><td>=INDEX($A$1:$D$3,A10,B10)</td></tr></table>

Then I sorted this column after getting all of the letters into a column.

But... is there a more elegant solution?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
With VBA?
Code:
Sub test()
Dim elem() As Variant
nc = Cells(1, Columns.Count).End(xlToLeft).Column
nr = Cells(Rows.Count, 1).End(xlUp).Row
nt = nc * nr
r = 1
c = 1
ReDim elem(nt)
For i = 1 To nt
elem(i) = Cells(r, c)
c = c + 1
If c = nc Then
c = 1
r = r + 1
End If
Next
For rw = 1 To nt
Cells(rw, 5) = elem(rw)
Next
With Worksheets("[COLOR=red]NAME[/COLOR]").Sort '<--- change with your sheet's name
        .SetRange Range(Cells([COLOR=red]1[/COLOR], [COLOR=red]5[/COLOR]), Cells(nt, [COLOR=red]5[/COLOR])) '<-- change row, column where you want result
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
End With
End Sub
 
Upvote 0
:outtahere:
Code:
Sub reorder()
Dim e, k As Long, c As Range
With Range("A1").CurrentRegion
For Each e In .Cells
    k = k + 1
    Cells(k + .Rows.Count + 2, 1) = "=" & e.Address
Next
Set c = Cells(.Rows.Count + 3, 1)
c.Resize(k).Sort c, 1
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,225
Members
452,896
Latest member
IGT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top