将 VBA 中的数字范围更改为负数

Change Range of Numbers to be Negative in VBA

我正在尝试在 excel 中编写一个宏。理想情况下,我会 select 一组带有数字的单元格并按下一个键,这些数字都会变成负数。这将为我节省大量时间,我将不胜感激。

Sub Make_Selection_Negative()
    Dim cell As Object

    For Each cell In Selection
        cell = cell - cell - cell
    Next cell

End Sub

尝试使用此代码进行选择:

Sub negative()

    Dim cel As Range
    Dim selectedRange As Range

    Set selectedRange = Application.Selection

    For Each cel In selectedRange.Cells
        If cel.Value > 0 Then
        cel.Value = cel.Value - (cel.Value * 2)
        End If
    Next cel

End Sub

或者一个单元格:

Sub negative()
if ActiveCell.Value > 0 then
ActiveCell.Value = ActiveCell.Value - (ActiveCell.Value * 2)
end if

End Sub

考虑:

Sub Negativize()
    Dim cel As Range
    For Each cel In Selection
        cel.Value = -Abs(cel.Value)
    Next cel
End Sub