VBA-将可变范围内的数字更改为负数

时间:2018-10-16 09:50:52

标签: excel vba excel-vba

我想在工作表中将一列更改为负数,因为该列表示“缺货”。

我从下面的链接中获取了代码,该代码会将给定范围的值更改为负数:

https://www.extendoffice.com/documents/excel/677-excel-change-positive-numbers-to-negative.html

但是问题在于,这将需要用户的互动。

代码:

Sub ChangeToNegative()
    'Updateby20131113
    Dim rng As Range
    Dim WorkRng As Range

    On Error Resume Next

    xTitleId = "KutoolsforExcel"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    Set WorkRng = WorkRng.SpecialCells(xlCellTypeConstants, xlNumbers)

    For Each rng In WorkRng
        xValue = rng.Value
        If xValue > 0 Then
            rng.Value = xValue * -1
        End If
    Next
End Sub

然后我发现将代码放入工作表本身,并命名子Change(ByVal Target As Range),它将在您使用它时更新所选范围。

代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim WorkRng As Range
    On Error Resume Next

    Set WorkRng = Application.Selection
    Set WorkRng = WorkRng.SpecialCells(xlCellTypeConstants, xlNumbers)

    If Target.Address = WorkRng Then     
        For Each rng In WorkRng
            xValue = rng.Value
            If xValue > 0 Then
                rng.Value = xValue * -1
            End If
        Next 
    End If
End Sub

这很好用,但是这意味着无论我单击哪个单元格并输入数字,它都是负数。

因此,我不想使用Application.Selection,而是要给它指定一个特定范围-但可以更改。

  1. 因此,仅当单元格C5:C143中有文本时,单元格F5:F143才应为负数

  2. 如果我删除C5:C143之间的任何单元格,则应该相应地更新范围。

也许范围可能基于C4C144中的文本-因此,F列中这两个文本单元格之间的值是否为负数?

1 个答案:

答案 0 :(得分:1)

我添加了很多注释来解释代码的作用。

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim WorkRng As Range
    Dim RangeToCheck As Range
    Dim rCell As Range

    'Don't Resume Next - if an error occurs handle it properly
    'and don't just hope the code can carry on regardless.
    On Error GoTo Err_Handle

    'This is the range we're looking at.
    'Use a named range so the range will update if you add/remove cells.
    Set RangeToCheck = Union(Range("Column_C_Figures"), Range("F5:F143"))

    'Are any cells within the required range?
    If Not Intersect(Target, RangeToCheck) Is Nothing Then

        'The cell will be updated, so disable events so
        'Worksheet_Change doesn't fire a second time.
        Application.EnableEvents = False

        'Look at each cell in Target.
        'More than one cell could change if values pasted in, or row deleted, or....
        For Each rCell In Target
            'All values in Target may not be in RangeToCheck so only look at
            'the ones that are.
            If Not Intersect(rCell, RangeToCheck) Is Nothing Then
                If IsNumeric(rCell) And rCell > 0 Then
                    rCell = rCell * -1
                End If
            End If
        Next rCell

    End If

Fast_Exit:

    Application.EnableEvents = True

Exit Sub

Err_Handle:
    'Deal with any errors and resume so that events are re-enabled.
    Select Case Err.Number
        'Case 13 'Example of error that may occur.
            'Deal with a data type mismatch and either
            'Resume, Resume Next or Resume Fast_Exit.
        Case Else
            Resume Fast_Exit
    End Select

End Sub