[英]Change plus to minus based on another cell value
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. 如果F列中的值(与金额在同一行中)为欠款,我希望I列(第9行)中的金额更改为减额(借方金额)。
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 我带了这部分来检查F列中的值
And Text.Offset("-3, 0") = "Debit-Outstanding"
I think a VBA macro is overcomplicated for what you need. 我认为VBA宏对于您所需要的过于复杂。
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. 如果是一次性的,请创建另一列,其中您有一个公式,例如“ = I9 *(IF(F9 =“ Debit-Outstanding”,-1,1)),向下复制到最后,然后将值复制到,删除计算出的列,仅此而已。
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! 希望有帮助!
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.