Making cells negative depending on neighbor cell

Vbanoob98

Board Regular
Joined
Sep 13, 2019
Messages
128
Hi guys, I have the following code that adds a cell value based on finding a text "Total" in the row. Im stuck now trying to make it change a value to negative

Here is the code
VBA Code:
Sub test()
 
myNum = Application.InputBox("Description")
LastRow = Range("C" & Rows.Count).End(xlUp).row

With Range("C2:C" & LastRow)


     Set c = .Find("*Total")
     If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.Offset(0, -1).Value = 555
             c.Offset(0, -2).Value = 123
             c.Offset(0, 4).Value = myNum
             c.Offset(0, 0).Value = make numbe negative code goes here
c.Offset(0, ).Value = Code here to multiple the # * -1
            Set c = .FindNext(c)
        If c Is Nothing Then
            GoTo DoneFinding
        End If
        Loop While c.Address <> firstAddress
      End If
DoneFinding:
End With

End Sub

Here is the part Im trying to change
Code:
c.Offset(0, 0).Value = make value
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What value are you trying to make the negative of?

c.Offset(0, 0) represents the cell you found the word "Total" in. Are you sure you want to replace that with a number?

Other than that - you appear to be on the right track, simply multiply the desired number by negative 1

For example, if you wanted the negative of the "myNum" variable, it'd be this;

VBA Code:
             c.Offset(0, 4).Value = myNum*-1

If myNum is already negative, that'd produce the positive value. If you always wanted the negative value ( 5 becomes -5 and -5 stays -5) then it'd be this;

VBA Code:
             c.Offset(0, 4).Value = ABS(myNum)*-1
 
Upvote 0
Offset is not required if Offset is ( 0, 0)
Value property is the default and therefore not required to be specified

This multiplies the number by -1
VBA Code:
c = -c

If the cell should always contain a negative number
VBA Code:
c = -Abs(c)
 
Upvote 0
Sorry its supposed to be (0, 1) and its a cell thats already there

I tried

VBA Code:
C.offset(0,1).value = cell.value * -1

But I get an error object requiered
 
Upvote 0
"cell" is not a declared object.

You still haven't said what value you're trying to show the negative of.

In your example, you have this;

1581073009800.png


What is the value a negative of? 555? 123? myNum? One of the blanks?
 
Upvote 0
Sorry, I was trying the solutions that were offered. I was just trying to convert numbers to negative. So I had a bunch of random totals 45557, 28373, etc

What I did was this:

VBA Code:
C.offset(0, 1).value = -abs(c.offset(0, 1))

Thanks for all the help :)
 
Upvote 0

Forum statistics

Threads
1,215,743
Messages
6,126,615
Members
449,322
Latest member
Ricardo Souza

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