VBA Code to Multiply by -1

allemanable

New Member
Joined
Nov 14, 2014
Messages
8
Hi everyone - I know I can use paste special values to multiply by -1 to change the sign, but I am trying to write a vba script that will do it for me. My file has sales data, but treats returns as a positive number. I need the script to find all "returns" line items and multiply the quantity and the sales dollars by negative 1.

1575843192152.png



This is only a subset of the the data. There are thousands of line items and the type may contain returns or sales.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Is this what you are looking for?
VBA Code:
Sub doit()
Dim v As Variant, i As Long
v = Selection
For i = 1 To UBound(v, 1)
    If LCase(v(i, 2)) = "returns" And v(i, 1) > 0 Then v(i, 1) = -v(i, 1)
Next
Selection = v
MsgBox "done"
End Sub

Select the data in columns labeled "Sales Values" and "Returns" before executing the macro.
 
Upvote 0
Here is another macro that you can consider...
VBA Code:
Sub MakeReturnsNegative()
  With Range("B2", Cells(Rows.Count, "B").End(xlUp))
    .Value = Evaluate("IF(" & .Offset(, 1).Address & "=""Returns"",-1,1)*" & .Address)
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,923
Members
449,094
Latest member
teemeren

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