VBA codes to convert number value in column D to negative based on corresponding Text in column B

Sean15

Well-known Member
Joined
Jun 25, 2005
Messages
698
Office Version
  1. 2010
Platform
  1. Windows
Can someone post VBA codes to convert number value in column D to negative if corresponding text in column B contains the word Credit or Payment.

Book1
ABCDE
1DateDescriptionClassCostCost to date
207/01/23Charge - RUBS Debra24.49(2,285.85)
307/01/23Credit - Valet Trash - Debra(9.07)(2,310.34)
406/02/23Payment - eCheck Mike(1,117.97)(2,301.27)
506/01/23Credit - RUBS - Electric - Debra(70.09)(1,183.30)
606/01/23Charge - RUBS - Electric - Debra32.57(1,113.21)
706/01/23Credit - Valet Debra(25.58)(1,145.78)
805/02/23Payment - eCheck Mike(1,132.58)(1,120.20)
905/01/23Charge - RUBS Debra12.3812.38
Sheet1
Cell Formulas
RangeFormula
E2:E9E2=E3+D2


Thank you.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
A bit puzzled by this as your numbers in red are already negative (they are just formatted to be red and in brackets)
 
Upvote 0
Sorry for the confusion. I changed values in column D myself to show the expected results. I want to automate because the worksheet can get quite large.
 
Upvote 0
Maybe this macro...
VBA Code:
Sub MakeCreditsPaymentsNegative()
  Dim R As Long
  For R = 2 To Cells(Rows.Count, "D").End(xlUp).Row
    If Left(Cells(R, "B").Value, 6) = "Credit" Or Left(Cells(R, "B").Value, 7) = "Payment" Then
      Cells(R, "D").Value = -Cells(R, "D").Value
    End If
  Next
End Sub
 
Last edited:
Upvote 0
.. or all at once?

VBA Code:
Sub Neg()
  With Range("D2", Range("D" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(search(""credit"",#))+isnumber(search(""Payment"",#)),-1,1)*" & .Address, "#", .Offset(, -2).Address))
  End With
End Sub
 
Upvote 0
I like VBA code in post #7.

Code works wells on small sample data. I noticed if code is run multiple times, value in column D changes each time. Since new data is added to worksheet each month, code will be executed each time data is added. My fault: I should have thought of it and presented it in post #1.

Could you adjust code to allow for: if value in column D is already negative (based on criteria), no change in value. Again, my apologies.
 
Upvote 0
Could you adjust code to allow for: if value in column D is already negative (based on criteria), no change in value.
Try
VBA Code:
Sub Neg_v2()
  With Range("D2", Range("D" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(search(""credit"",#))+isnumber(search(""Payment"",#)),-1,1)*abs(" & .Address & ")", "#", .Offset(, -2).Address))
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,216,167
Messages
6,129,266
Members
449,497
Latest member
The Wamp

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