0

I got this code on the internet.

I want the amounts in column I (from row 9) to change to a minus amount (debit amount) if the value in column F (would be in the same row as the amount) is Debit-Outstanding.

Sub test()
Dim rcel As range
Dim Rng As range
Set Rng = range(ActiveCell, Cells(Rows.Count, _
ActiveCell.Column).End(xlUp))
For Each rcel In Rng
If rcel.Value <> "" And rcel.Value >= 0 Then
rcel.Value = rcel.Value * -1
End If
Next rcel
End Sub

I brought in this part to check the value in column F

And Text.Offset("-3, 0") = "Debit-Outstanding"

4

2 回答 2

2

I think a VBA macro is overcomplicated for what you need.

If it's a one-off - just create another column where you have a formula like "=I9*(IF(F9="Debit-Outstanding",-1,1)), replicate down to the end, and then copy values over, remove the calculated column, and that's it.

If the "Debit-Outstanding" values are likely to change, or you will be adding data, just incorporate the column with formula above into your workbook, and use that instead of the current column I containing values.

Hope that helps!

于 2013-10-08T10:32:39.663 回答
1

Give this a try:

Sub test()
    Dim rcel As Range
    Dim Rng As Range
    Dim rw As Long
    Set Rng = Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
    For Each rcel In Rng
        With rcel
            rw = .Row
            If .Value <> "" And .Value >= 0 And Cells(rw, "F") = "Debit-Outstanding" Then
                .Value = -.Value
            End If
        End With
    Next rcel
End Sub
于 2013-10-08T10:19:13.697 回答