Teach Microsoft Office
Online Video Tutorials for Microsoft Office
Microsoft Office Excel Macro

Reverse Row or Column Order in a Worksheet

Bookmark and Share
This macro reverses the order of a selected row or column in excel. It will completely reverse the data.

This is different than using the data > sort or filter commands because those will re-arrange the columns or rows. This macro will completely mirror a column or row in terms of the data. The one thing to remember is that it will not work for an entire row or column, but only one cell less than the entire row or column.

To use it, simply select a range of cells in one row or one column and then run the macro. Since it cannot work on multiple columns or rows at once, it is advised to assign this macro to a keyboard shortcut or a toolbar button in excel.
Macro Installation Location: Module
Keywords: sort data sorting excel one column next subset adjacent data set table advanced
Reverse Rows or Columns in a Worksheet
Public Sub Reverse_Rows_or_Columns()

'This Macro will reverse a selection of rows or columns.
'Note: you cannot select an etire row or column, but one
'cell less than that will work fine.
'Don't forget to assign this macro a keyboard shortcut or
'a toolbar button.

Dim Arr() As Variant
Dim Rng As Range
Dim C As Range
Dim Rw As Long
Dim Cl As Long

On Error GoTo EndMacro

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

Set Rng = Selection
Rw = Selection.Rows.Count
Cl = Selection.Columns.Count
If Rw > 1 And Cl > 1 Then
MsgBox "Must select either a range of rows or columns, but not simultaneaously columns and rows.", _
vbExclamation, "Reverse Rows or Columns"
Exit Sub
End If

If Rng.Cells.Count = ActiveCell.EntireRow.Cells.Count Then
MsgBox "Can't select an entire row, only up to one cell less than an entire row.", vbExclamation, _
"Reverse Rows or Columns"
Exit Sub
End If
If Rng.Cells.Count = ActiveCell.EntireColumn.Cells.Count Then
MsgBox "Can't select an entire column, only up to one cell less than an entire column.", vbExclamation, _
"Reverse Rows or Columns"
Exit Sub
End If

If Rw > 1 Then
ReDim Arr(Rw)
Else
ReDim Arr(Cl)
End If

Rw = 0
For Each C In Rng
Arr(Rw) = C.Formula
Rw = Rw + 1
Next C

Rw = Rw - 1
For Each C In Rng
C.Formula = Arr(Rw)
Rw = Rw - 1
Next C

EndMacro:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True

End Sub

 
Official PayPal Seal SSL Join TeachMsOffice.com on Facebook Follow TeachMsOffice.com on Twitter


Microsoft Office Tutorials | Office Tutorials - Excel Word PowerPoint | HD Tutorial Video Player Overview | About TeachMsOffice.com

TeachMsOffice.com provides HD Online Video Tutorials and Training for Microsoft Office programs such as Excel, Word, and PowerPoint. We use a specialized video player interface to teach a vast list of Microsoft Office Tutorials and we add new tutorials on a weekly or monthly basis.