Make positive value negative if a cell contains

rsutton1981

New Member
Joined
Mar 9, 2016
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hi,

Our accounting software exports all values as positive even if it is a credit.

I have tried and failed to make some VBA to do this by it fails

The target is "SC" in Column B from row 15 onwards.
if "SC" is noted then the values in K, L and O should be changed to negative.

using some other VBA code previously used I have got this but it fails
VBA Code:
Sub reverse_SC()

' make value negative with MyTarget

Const MyTarget = "SC" ' <-- change to suit

Dim Rng As Range
Dim I As Long, j As Long

' Calc last row number
'j = Cells.SpecialCells(xlCellTypeLastCell).Row 'can be:

j = Range("B" & Rows.Count).End(xlUp).Row

' Collect rows with MyTarget
For I = j To 1 Step -1
If WorksheetFunction.CountIf(Rows(I), MyTarget) > 0 Then
If Rng Is Nothing Then
Set Rng = Rows(I)
Else
Set Rng = Union(Rng, Rows(I))
End If
End If
Next

' Change cell with MyTarget
If Not Rng Is Nothing Then Columns ("k") xValue *-1



' Update UsedRange
  With ActiveSheet.UsedRange: End With

End Sub

Please help
 

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.
I assume the accounting software exports a file that you're then copying and pasting into excel. The best way to solve your problem is to import into excel via PowerQuery. It will give you a vastly superior way of managing the import that will work for each and every export automatically (well, okay, you need to remember to refresh the data).

On the 'Data' Ribbon, select 'New Query' on the Get & Transform tab and chose from file. It might take a little learning, but it'll definitely be worth it.
 
Upvote 0
if you want to use VBA until you've got the hang of PowerQuery (I can't praise it highly enough) then try this:

VBA Code:
Sub reverse()
    Dim rCode As Range
    Dim cTarget As Range
    
    Set rCode = Range("B15", Range("B15").End(xlDown))
    
    For Each cTarget In rCode
        If cTarget = "SC" Then
            cTarget.Offset(0, 9) = -cTarget.Offset(0, 9)
            cTarget.Offset(0, 10) = -cTarget.Offset(0, 10)
            cTarget.Offset(0, 13) = -cTarget.Offset(0, 13)
        End If
    Next
End Sub
 
Upvote 0
Hi Peter,

had to adjust the range element, it did not seem to work but all worked. it was a much neater bit of code. Thanks
 
Upvote 0
excellent, glad I was able to help and thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

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