reversing the order of 2 columns

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
Is there any way/trick to reverse a table without using sorting? Here is my table

From

[TABLE="class: grid, width: 128"]
<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]average[/TD]
[TD="width: 64"]grade[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD]f[/TD]
[/TR]
[TR]
[TD="align: right"]50[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD="align: right"]60[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD="align: right"]80[/TD]
[TD]a[/TD]
[/TR]
</tbody>[/TABLE]

To

[TABLE="class: grid, width: 128"]
<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]average[/TD]
[TD="width: 64"]grade[/TD]
[/TR]
[TR]
[TD="align: right"]80[/TD]
[TD]a[/TD]
[/TR]
[TR]
[TD="align: right"]60[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD="align: right"]50[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD]f[/TD]
[/TR]
</tbody>[/TABLE]

Thank you very much.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
One way, copied across and down

Excel Workbook
ABCDE
1averagegrade
20f80a
350c60b
460b50c
580a0f
Reverse
 
Upvote 0
VBA?
Code:
Sub revers()
    Dim a As Variant
    Dim i, j, lr, t As Integer
    lr = Cells(Rows.Count, 1).End(xlUp).Row - 1
    a = Cells(2, 1).Resize(lr, 2)
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    t = 1
    For i = lr To 1 Step -1
        For j = 1 To 2
            b(t, j) = a(i, j)
        Next
        t = t + 1
    Next
    Cells(2, 3).Resize(lr, 2) = b
End Sub
 
Last edited:
Upvote 0
Thanks all for the help. Not prohibited but I thought there is another way I do not know off. Thanks all once again
You're welcome.

.. and of course if the data started out in a jumbled order then sorting wouldn't reverse the table anyway. :)
 
Last edited:
Upvote 0
If you are interested, here is a vba way to reverse a table (but keep the headings) without looping through the rows and columns.

Rich (BB code):
Sub Reverse_Table()
  Dim rws As Long, cols As Long
  Dim vRws As Variant, vCols As Variant
  
  With Range("A1").CurrentRegion
    rws = .Rows.Count
    cols = .Columns.Count
    vRws = Application.Transpose(Split("1 " & Join(Application.Transpose(Evaluate(rws + 1 & "- row(1:" & rws - 1 & ")")))))
    vCols = Application.Transpose(Evaluate("row(1:" & cols & ")"))
    .Offset(, cols + 1).Value = Application.Index(.Value, vRws, vCols)
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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