Flipping Data

Written by Allen Wyatt (last updated June 6, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


2

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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Restoring the Analysis ToolPak

Add-ins for Excel, such as the Analysis ToolPak, are stored in files on your hard drive that can be deleted. If you ...

Discover More

Updating Multiple PivotTables at Once

PivotTables are a great way to process huge amounts of data and make sense of that data. If you have a number of ...

Discover More

Adding Quoted Words to an Index

It is not unusual to need to convert one notation in a document into another entirely different notation. For instance, ...

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)

Pasting without Updating References

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

Discover More

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
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}] (all 7 characters, in the sequence shown) 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 8 + 1?

2020-11-17 11:13:32

Amos

Hi Allen,

Thanks for that, its very useful and time saving solution.
One question though, Is there any way to auto select all data and Flip it, without any input dialog?


2020-06-06 17:26:46

Donald Berg

You should have offered, instead of a newbie inserting and then typing in a new column of numbers (as some might) a simpler method to enter numbers into a new column: Enter 1 into the new column on the top row, then click return, then then rt click and drag the small lower right box in the cell and select "fill series"". Then do the rest to traspose. If you want to transpose from a vertical column to a horizontal one just use the dialogue box (for copy/paste) by selecting rt clicked paste special


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.