Search TeachExcel.com
TeachExcel.com
TE
Teach Excel MS Office Tutorials Excel Consulting Services Excel Forum
Excel Video Tutorials Excel Tips Free Excel Macros Excel Help Resources Contact TeachExcel
Video Tutorials
  • Free Macros
  • Excel Help Directory
  • Excel 2007 Resources
  • Keyboard Shortcuts
  • Excel Forum
  • Contact/About

Macros
Excel Tutorials For Macros

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.
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


Bookmark and Share


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.

      For Excel Versions Prior to Excel 2007
      Go to Tools > Macros > Visual Basic Editor

      For Excel 2007
      Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic

  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.

    1. Go to the menu at the top of the window and click Insert > Module
    2. Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
    3. Go to Step 8.

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

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Go to Step 8.

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

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
    5. Go 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.



Similar Helpful Excel Resources

Can I Reverse The Order Of Values In A Column Or Row? - Excel

View Content
Can I reverse the order of values in a column or row? [ie] first cell goes to last cell, last cell goes to first cell, middle cell stays where it is, etc. [and if so, how?].

Thanks

How Can I Reverse The Order Of A Column Of Numbers . . . - Excel

View Content
I want to turn a long column of numbers upsidedown so that the last entries
become the first vice versa.


How Can I Reverse The Order Of A Column Of Numbers . . . - Excel

View Content
I want to turn a long column of numbers upsidedown so that the last entries
become the first vice versa.


Reverse Order Of Numbers In A Column - Excel

View Content
How can I reverse the order of numbers in a column?
i.e. My column contains [1 4 6 3 2] and I want it to contain [2 3 6 4 1].

Thank you.


Reverse Column Order With A Macro - Excel

View Content
For my job, I frequently need to reverse the order of columns in Excel.
I'll get a feed of financial data that goes 2006 2005 2004 2003 (etc),
and I need to make it go 2003 2004 2005 2006. The good news is that I
know how to do this by sorting. The bad news is that I spend 5% of my
work week doing it. I know it's just a few keystrokes, but it's a
hassle, and they add up over time. I should also note that the data
format is sometimes unusual, so I almost always have to create a helper
row. However, the data always comes to me in exactly the reverse of the
order that I want it. In other words, I do not need to "sort," as much
as I need to "reverse."

I'd like to be able to simply highlight the rows and use a shortcut.
Does there exist a routine to do this for me? If not, does there exist
a similar routine that could easily be modified?


Thank you.



How Do I Reverse The Order Of My Entries In A Column? - Excel

View Content
Hello everyone!!! I've come across a problem in excel which I was hoping you guys/girls can help me with.

Basically, I have downloaded some historical data into excel. Inconveniently for me, the dates are listed in reverse order, starting from the today's date and going back...

...i.e., Column A looks like this...

11/10/10
10/10/10
09/10/10
.
.
.
01/01/10

I was wondering whether the order of the entries can be reversed, i.e., transforming column A to look like this...

01/01/10
02/01/10
03/01/10
.
.
.
11/10/10

Any help is greatly appreciated. Many thanks!

Reverse The Order Of Data In A Column - Excel

View Content
The requirement is to write a macro for generating a report in columnar form for checking ticket solving efficiency.. The macro i have generates a new sheet with the report but column A in sheet 1 (report) is in reverse order.. How do i put the last cell value into the first one and so on??

I have attached the workbook (in a zipped form bcos .xls is greater than 100kb)..

Pls help me!!

Paste Column Values In Reverse Order - Excel

View Content
How do I transpose the values in a column of cells (so the value in the topmost cell in the old column becomes the value in the bottommost cell in a new column)? Thank you.

I Typed A Column In Reverse Order, How Do I Flip It (upside Down). - Excel

View Content
How do I flip a column of numbers, to reverse the order.



Column Headers In My Excel Sheet Are In Reverse Order-reg - Excel

View Content
is there any tip to change this to original position?



Random Tutorials
Net Present Value / Discounted Cash Flow Calculations
(Intermediate)
Absolute and Relative Cell References
   - & INDIRECT() Function Introduction

(Easy)
Link Cells Between Worksheets
(Easy)
How to record a Macro - And what One is
(Easy)
Assign a Macro to a Button and Toolbar
(Intermediate)
Assign Keyboard Shortcuts to Macros
(Easy)
Submit Inquiry Here
  • Prices From $10
Name:*
E-mail:*
Request:*
The macro(s) on this page will be sent with the request.
Contact | Privacy Policy | Disclaimer
Copyright© 2012 TeachExcel.com