Macro to change numbers to negative when entered into a row

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,

I have a range of B29:AZ32

I would like a macro the if a number is entered into and cell in this range that it makes it a negative

I.e if some put in 1.6 it changes it to -1.6
if they put in -1.6 it stays as it is

please help if you can

Thanks

Tony
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
how about this?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("B29:AZ32"), Target) Is Nothing Then
        If Target.Value > 0 Then Target.Value = Target.Value * -1
    End If
End Sub
 
Upvote 0
how about this?
As the Intersect result may not be the same as Target so it must be :​
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
       Dim Rg As Range, Rc As Range
       Set Rg = Intersect([B29:AZ32], Target)
    If Not Rg Is Nothing Then
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        For Each Rc In Rg
            If Rc.Value2 > 0 Then Rc.Value2 = -Rc.Value2
        Next
            .EnableEvents = True
            .ScreenUpdating = True
        End With
            Set Rg = Nothing
    End If
End Sub
 
Last edited:
Upvote 0
The original code corrected for an unique cell entry :​
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 Then If Not Intersect(Range("B29:AZ32"), Target) Is Nothing Then _
                                  If IsNumeric(Target.Value2) Then If Target.Value2 > 0 Then Target.Value2 = -Target.Value2
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

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