簡體   English   中英

復制並粘貼為負值

[英]copy and paste as negative value

我開發了下面的代碼,我希望它應該將值粘貼為負值而不是正值。 我環顧四周,但沒有發現類似的東西。

它應該是負的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

簡單的乘法:

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

我認為你可以通過只改變一行來做到這一點。 試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM