复制并粘贴为负值

时间:2021-06-29 17:06:34

标签: excel vba

我开发了下面的代码,我希望它应该将值粘贴为负值而不是正值。我环顾四周,但没有发现类似的东西。

应该是负数ws.Cells(r, "I").Value

Sub copyandpasteasnegative()

Dim ws          As Worksheet, LastRow As Long, r As Long
Set ws = Sheet1

LastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
    
For r = 2 To LastRow

If ws.Cells(r, "B") = "KIO" Then

ws.Cells(r, "I").Value = ws.Cells(r + 1, "I").Value

End If
Next

End Sub

2 个答案:

答案 0 :(得分:1)

简单的乘法:

ws.Cells(r, "I").Value = -1 * ws.Cells(r + 1, "I").Value

可能最好在尝试乘法之前使用 IsNumeric

With ws.Cells(r + 1, "I")
    If IsNumeric(.Value) Then
        ws.Cells(r, "I").Value = .Value * -1 
    End If
End With

答案 1 :(得分:0)

我认为您只需更改一行就可以做到这一点。试试这个:

ws.Cells(r, "I").Value = 0 - ws.Cells(r + 1, "I").Value