Reverse Row or Column Order in a Worksheet

Add to Favorites
Author:

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.

Where to install the macro:  Module

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





Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons 50+ Hours of Instruction 200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Similar Content on TeachExcel
Get the Row or Column Number of a Cell in Excel
Tutorial: How to get the row or column number of the current cell or any other cell in Excel. This t...
Count the Occurrences of the Largest or Smallest Value in a Range
Tutorial: How to count the occurrence of the largest or smallest value in a range; basically, how m...
Show All Formulas in a Worksheet in Excel
Tutorial: Display all formulas instead of their output values. This allows you to quickly troubles...
Print All Charts That are in a Worksheet
Macro: This macro will print all of the embedded charts which are on the current active works...
Delete All Comments in a Worksheet in Excel Macro
Macro: Excel macro that will delete all of the comment contained within the active or current wor...
Quickly Reset All Form Controls in a Worksheet in Excel
Tutorial: How to reset all Form Controls on a worksheet in Excel with the click of a button or two ...


How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.

Tutorial Details
Similar Content
Get the Row or Column Number of a Cell in Excel
Tutorial: How to get the row or column number of the current cell or any other cell in Excel. This t...
Count the Occurrences of the Largest or Smallest Value in a Range
Tutorial: How to count the occurrence of the largest or smallest value in a range; basically, how m...
Show All Formulas in a Worksheet in Excel
Tutorial: Display all formulas instead of their output values. This allows you to quickly troubles...
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course