Adding a Minus Sign with a macros

DaleKeel

Board Regular
Joined
Sep 11, 2019
Messages
56
Office Version
  1. 2013
Platform
  1. Windows
I need to add a minus sign in front of a number in cell G8 and one in H8.

Thank you for your time
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Code:
Sub Main()
  Dim c As Range
  For Each c In [G8:H8]
    If c > 0 Then c = c * -1
  Next c
End Sub
 
Upvote 0
Here is another macro (a one-liner) that will also do what you want...
Code:
Sub Main()
  [G8:H8] = [IF({1},-ABS(G8:H8))]
End Sub
 
Last edited:
Upvote 0
Here is another macro (a one-liner) that will also do what you want...
Code:
Sub Main()
  [G8:H8] = [IF({1},-ABS(G8:H8))]
End Sub
Is the IF function necessary?
This seems to work:
Code:
Sub Main()
[G8:H8] = [-ABS(G8:H8)]
End Sub
 
Upvote 0
Is the IF function necessary?
This seems to work:
Code:
Sub Main()
[G8:H8] = [-ABS(G8:H8)]
End Sub

Yes, it is necessary. Without it, if you put two different numbers in G8 and H8, the number in G8 ends up in both cells.
 
Upvote 0
Yes, it is necessary. Without it, if you put two different numbers in G8 and H8, the number in G8 ends up in both cells.

Hi Rick, I put 36 in G8 and 27 in H8 and got -36 and -27 respectively in the cells with the code by footoo :confused: Can you elaborate on when you get the same number in each cell.
 
Upvote 0
Hi Rick, I put 36 in G8 and 27 in H8 and got -36 and -27 respectively in the cells with the code by footoo :confused: Can you elaborate on when you get the same number in each cell.
Weird... when I do the same thing, I get -36 in both cells. :confused:

Maybe it is version related... I am using XL2010, what version are you using?
 
Upvote 0
Currently 365 but if I get a chance over the weekend I'll see if I get the same results with 2010.
eD1AgFp.jpg

t3VTcDy.jpg
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top