Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Creating a Plus/Minus Button.

Creating a Plus/Minus Button

by Allen Wyatt
(last updated June 19, 2018)

10

On some calculators there is a little button that can come in very handy: the plus/minus button. This button, when pressed, will switch whatever value is on the display between its positive and negative values. For instance, if the display shows the number 57, then pressing the button will change the display to -57. Pressing it again will switch the value back to 57.

If you would like a "button" that does this in Excel, you'll quickly find that there is none built into the program. You can quickly create one, however, by using a macro:

Sub PlusMinus()
    Dim cell As Range

    For Each cell In Selection
        If Application.IsNumber(cell) Then
            If Not(IsDate(cell)) Then
                cell.Value = cell.Value * -1
            End If
        End If
    Next cell
End Sub

Note that the macro simply steps through whatever range of cells you selected when the macro started. Each cell is checked to see if it contains a number. If it does, then the cell is checked to make sure it doesn't contain a date. Only then is the value of the cell multiplied by -1. The result is a switch in sign for the number.

The two checks done on the cell are important so that you don't mess up the contents of cells by accident. The first check (using the IsNumber function) looks to see if the cell contains a number. When would a cell not contain a number? The most critical time is when it contains a formula; you don't want to mess those up. The second check uses the IsDate function, which checks to see if the cell contains a date. This is necessary because a cell can contain a number that actually represents a date, and you don't want to change those dates to minus values.

You can assign this macro to a shortcut key or add it to the Quick Access Toolbar to make it easy to use at any time.

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 (9271) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Creating a Plus/Minus Button.

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

Turning Off Automatic Bulleted Lists

As you are typing away on a document, you may notice that Word automatically formats bulleted lists (or what it thinks ...

Discover More

Counting Times within a Range

Excel allows you to easily store dates and times in your worksheets. If you have a range of cells that contain times and ...

Discover More

Jumping to the Ends of Table Rows

Need to jump from one end of a table row to another? Word provides a couple of handy shortcuts that can make this type of ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Selecting a Cell in the Current Row

Macros often need to select different cells in a worksheet. Here's how you can use macro commands to change which cell is ...

Discover More

Using R1C1 Formula References in a Macro

Besides the regular way of displaying formulas, Excel can also display them using what is called R1C1 format. If you are ...

Discover More

Page Numbers in VBA

When you print a larger worksheet, Excel breaks the printout across several pages. You may want to know, before you ...

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 2 + 4?

2018-05-07 16:39:53

Henk

Hi Peter, what is this "c.c" bit?


2018-03-23 13:24:43

Peter Atherton

Nicely spotted Saskia,

Variation on Willy's should do it

Sub PlusMinus()
Dim cell As Range
On Error Resume Next 'copes with cells that are not numeric
For Each cell In Selection
if instr(1,cell.text,":") >0 then
c.c
ElseIf Not cell.HasFormula Then
cell.Value = -cell.Value
end if
Next cell
End Sub


2018-03-23 13:04:21

Willy Vanhaelen

@Saskia
By default Excel uses the 1900 date system which doesn't support negative times. So ###### is displayed.
You can use negative times if you check the 1904 date sytem in the Excel Options (Advanced , When calculating this workbook:) but that will give problems with dates copied from workbooks using the default 1900 date system.


2018-03-22 03:38:56

Saskia Jacobsen

It goes wrong when the content of a cell is a time...!


2014-05-06 14:56:25

Willy Vanhaelen

@Micky: your solution does erase the formula.

@Don: "On Error GoTo 0" is not needed in this case because there is no code following the statement that can cause the error. After ending the "For Each" follows the "End Sub" and that resets the On Error anyway.


2014-05-05 13:15:16

Don

I had the same problem with it erasing the formula. I agree with Willy that the Cell.HasFormula is the key and WRosocha's suggestion to use "-cell.Value".

However, I do have an issue using "On Error Resume Next" to replace condition validation. Further heartburn when it comes to Functions and Subs that have an opening "On Error Resume Next" and no "On Error GoTo 0". When "On Error Resume Next" is used it just be just prior to the code line(s) where errors are acceptable. Immediately after should be the "On Error GoTo 0".

I also prefer using the VBA native functions instead of Application or Worksheet function. I've added a Len(Trim(cell.Value)) > 0 for the difference between the two.

That rant aside, here's my suggestion:
Sub PlusMinus()
Dim cell As Range
For Each cell In Selection
If Len(Trim(cell.Value)) > 0 _
And IsNumeric(cell) _
And Not cell.HasFormula _
And Not IsDate(cell) Then
cell.Value = -cell.Value
End If
Next cell
End Sub


2014-05-05 07:12:29

Michael (Micky) Avidan

@Willy,
This is a little shorter and seems 2 B working fine:
Sub PlusMinus()
On Error Resume Next
For Each CL In Selection
CL.Value = -CL
Next
End Sub
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL


2014-05-04 13:44:58

WRosocha

Willy,

Thank you for the additional insights.


2014-05-04 08:32:16

Willy Vanhaelen

This tip claims that formulas are left intact. That is not the case. When a cell contains a formula this macro replaces it with it's negative result. So the formula is gone. To prevent this we have to test whether the cell contains a formula.

'cell.Value = -cell.Value' generates an error when cell.Value isn't a number. So let's use this in this simplified macro that does what it is expect to do:

Sub PlusMinus()
Dim cell As Range
On Error Resume Next 'copes with cells that are not numeric
For Each cell In Selection
If Not cell.HasFormula Then cell.Value = -cell.Value
Next cell
End Sub

Cells containing a formula, a label or a date are left untouched.


2014-05-03 10:55:13

WRosocha

Why would you invoke the MULTIPLY operation to flip the sign bit?

This violates the principle of using the simplest programming constructs. It also invokes heavy duty computation in the CPU.

Why not use

cell.Value = -cell.Value

Are there cases where this wouldn't work?


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.