Reversing Integer Values

Written by Allen Wyatt (last updated September 25, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


Katy has a worksheet that, in some columns, contains integer data values ranging from 1 through 5. (In those particular columns, these are the only values in the column.) She would like to "reverse" these data values, such that 5 becomes 1, 4 becomes 2, 3 stays the same, 2 becomes 4, and 1 becomes 5. She only wants this to happen in certain columns, and she is stuck trying to figure out how to accomplish the task.

There are actually several different ways you can accomplish this task. The first is to use a simple formula to do the reversal. Assume, for instance, that the values you want to reverse are in column A, starting in row 2. The following formula will work:

=6-A2

Copy the formula down as far as you need, and the result is reversed values. You can then copy the values and paste them wherever you want using Paste Special. (You use Paste Special so you can copy just the values, not the formulas that create the values.)

This isn't the only formula you could use, however. You could use the CHOOSE function in this manner:

=CHOOSE(A2,5,4,3,2,1)

Another approach is to set up the numbers 1-5 in a difference set of cells (let's say you put those values in cells T1:T5) and then use the RANK function, in this manner:

=RANK(A2,T1:T5,0)
=RANK.EQ(A2,T1:T5,0)

Both return the desired reversal, but the second example (RANK.EQ) should be used in Excel 2010 and later versions because the RANK function was deprecated after Excel 2007.

You could also reverse the values in place by using a macro to do the task. Here's a simple one that will do the job:

Sub ReverseNumbers()
  Dim rCell As Range
  For Each rCell In Selection
    With rCell
      If IsNumeric(.Value) Then
        .Value = 6 - .Value
      End If
    End With
  Next
End Sub

To use the macro, select the cells you want to reverse and then run it. It first checks to make sure that the cell contains a numeric value; if so, it subtracts the value in the cell from 6, effectively reversing the value.

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 (13140) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

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

Blocking the First Sentence from File Info

When you save a file, the information from the start of the file is saved in the properties for the document, and can be ...

Discover More

Making Spell Check Ignore Characters

The rules of professional editing often require that editorial changes in a quote be noted with brackets. These brackets, ...

Discover More

Entering Info into Multiple Cells

Want to make an entry of the same value into a group of selected cells? It's easy to do with just one small change in how ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Calculating Monthly Interest Charges

Trying to calculate how much people owe you? If you charge interest or service charges on past-due accounts, there are a ...

Discover More

Starting Out Formulas

When you enter a formula from the keyboard, Excel only knows it is a formula if you start it with an equal sign. You can ...

Discover More

Generating Random Strings of Characters

If you need to generate a random sequence of characters, of a fixed length, then you'll appreciate the discussion in this ...

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 eight less than 8?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.