Macro to convert positive numbers to negative numbers in adjacent column if a criteria is met

Hi,

A              B                     C                      D                     E                      F
Sample      HENDRIK      2015-07-31      R 10,600      0      R 54,400      0      R 435,000
Test       HENDRIK      2015-07-31      R 10,600      0      R 54,400      0      R 435,000
Sample      HENDRIK      2015-07-31      R 6,300      0      R 32,700      0      R 261,000
Test        HENDRIK      2015-07-31      R 6,300      0      R 32,700      0      R 261,000
Test       HENDRIK      2015-07-31      R 6,300      0      R 32,700      0      R 261,000
Sample      HENDRIK      2015-07-31      R 6,300      0      R 32,700      0      R 261,000
Sample      HENDRIK      2015-07-31      R 7,900      0      R 38,100      0      R 304,000
Sample      HENDRIK      2015-07-31      R 7,900      0      R 38,100      0      R 304,000
Sample      HENDRIK      2015-07-31      R 7,900      0      R 38,100      0      R 304,000

In the above example , I have a spreadsheet and whenever the column A has Test, the macro must convert Columns D, E and F to negative
MasteruhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:
Is the "R " prefix before the values in columns [D], [E], & [F] actually present in the respective cells, or is this a cell formatting character (& the actual contents of the cells are just numeric values)?

Also, your data above seems to have eight columns of values, but only five column headings.

Could you upload/attach an example set of data in a workbook, please?

Thank you.
0
Rgonzo1971Commented:
Hi,

pls try

Sub macro()

For Each c In Range(Range("A1"), Range("A" & Cells.Rows.Count).End(xlUp))
    If c = "Test" Then
        c.Offset(0, 3) = -c.Offset(0, 3)
        c.Offset(0, 4) = -c.Offset(0, 4)
        c.Offset(0, 5) = -c.Offset(0, 5)
    End If
Next
End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
[ fanpages ]IT Services ConsultantCommented:
...If any values in columns [D], [E], or [F] are already negative, should they remain negative, or change to positive (especially if the code to convert the values is run more than once on the same set of data)?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Try this.....
Sub ChangeToNegativeValues()
Dim lr As Long
Dim rng As Range, cell As Range

lr = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("A2:A" & lr)

For Each cell In rng
    If LCase(cell) = "test" Then
        If Cells(cell.Row, "D") > 0 Then Cells(cell.Row, "D") = -Cells(cell.Row, "D")
        If Cells(cell.Row, "E") > 0 Then Cells(cell.Row, "E") = -Cells(cell.Row, "E")
        If Cells(cell.Row, "F") > 0 Then Cells(cell.Row, "F") = -Cells(cell.Row, "F")
    End If
Next cell
End Sub

Open in new window

0
Rgonzo1971Commented:
or to change only positive numbers

Sub macro()

For Each c In Range(Range("A1"), Range("A" & Cells.Rows.Count).End(xlUp))
    If c = "Test" Then
        c.Offset(0, 3) = -Abs(c.Offset(0, 3))
        c.Offset(0, 4) = -Abs(c.Offset(0, 4))
        c.Offset(0, 5) = -Abs(c.Offset(0, 5))
    End If
Next
End Sub

Open in new window

0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.