Flipping Data

by Allen Wyatt
(last updated January 5, 2017)

6

Many people use Excel as a simple database manager, entering information in different rows of a worksheet. As you are working with your data tables, you may come across a need to reverse the order of the rows in the table. Thus, if you have a table with ten rows, the rows would go from ten to one instead of one to ten.

There is no intrinsic function in Excel that allows you to flip data in this manner. However, you can use the sorting capabilities of Excel to accomplish the same thing by following these general steps:

  1. Insert a new column immediately to the left of your data table.
  2. In the cells of the new column, enter the numbers 1 through however many rows there are in your table.
  3. Select the rows that make up your data table.
  4. Choose Sort from the Data menu. Excel displays the Sort dialog box. (See Figure 1.)
  5. Figure 1. The Sort dialog box.

  6. In the Sort By drop-down list, indicate you want to sort by your newly created column.
  7. Click Descending as the type of sort.
  8. Click on OK. Excel reorders your data in the reverse order of what it was.

If you have to do a lot of data flipping on a daily basis, using the above steps can get rather tiring. In this case, you may want to create a macro to do the job for you. The following macro, FlipRows, will do the trick:

Sub FlipRows()
    Dim vTop As Variant
    Dim vEnd As Variant
    Dim iStart As Integer
    Dim iEnd As Integer
        Application.ScreenUpdating = False
        iStart = 1
        iEnd = Selection.Rows.Count
        Do While iStart < iEnd
            vTop = Selection.Rows(iStart)
            vEnd = Selection.Rows(iEnd)
            Selection.Rows(iEnd) = vTop
            Selection.Rows(iStart) = vEnd
            iStart = iStart + 1
            iEnd = iEnd - 1
        Loop
        Application.ScreenUpdating = True
End Sub

In order to use this macro, all you need to do is select the rows you want flipped and run it. The macro will not change your data, other than flipping the rows. In other words, it will not add any columns of information.

An interesting feature of this approach is that you can quickly adapt it to flipping columns of data. All you need to do is change all occurrences of the word "Rows" to "Columns." Thus, the following becomes the new macro:

Sub FlipColumns()
    Dim vTop As Variant
    Dim vEnd As Variant
    Dim iStart As Integer
    Dim iEnd As Integer
        Application.ScreenUpdating = False
        iStart = 1
        iEnd = Selection.Columns.Count
        Do While iStart < iEnd
            vTop = Selection.Columns(iStart)
            vEnd = Selection.Columns(iEnd)
            Selection.Columns(iEnd) = vTop
            Selection.Columns(iStart) = vEnd
            iStart = iStart + 1
            iEnd = iEnd - 1
        Loop
        Application.ScreenUpdating = True
End Sub

Again, simply select the columns you want to flip and then run the macro.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2653) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Changing Your Company Name

The installation process for Office and Excel allows you to specify a company name. If your company name later changes, you ...

Discover More

Adding a Macro to the Quick Access Toolbar

The Quick Access Toolbar is a great place to put anything for which you want quick access. (Duh!) Here's how you can add your ...

Discover More

Changing Fonts in Data Validation Drop-Down Lists

The data validation capabilities of Excel allow you to easily create drop-down lists showing what data is acceptable for a ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Quickly Transposing Cells

If you want to turn a range of cells by 90 degrees within a worksheet, you need to understand how Excel can handle the ...

Discover More

Moving Cells Using the Mouse

Want to easily move information from one cell to another? A quick way to do it is to simply drag and drop using the mouse.

Discover More

Pasting without Updating References

Do you need to paste formulas without updating the references in whatever you are pasting? You can accomplish this, depending ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 + 3?

2016-11-27 06:58:17

miha

Please, how I can flip cells/columns in oly one row?


2016-05-30 05:29:41

Nawhaal

WOW! Such a simple logical step - THANKS SO MUCH!


2015-08-05 19:15:21

AG

Works a treat! Thank you


2015-07-15 08:47:13

Lenneke

Thank you!! This will save me so much time, effort and errors! Hero.


2014-07-25 17:26:39

arif

thank you thank you thank you.
i had some old training in VBA for excel and your code to reverse columns worked flawlessly for me.


2014-02-28 15:42:14

Ian

ran to flip rows ok but all fomula in cells are gone, only numbers are left.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.