首页 文章

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

提问于
浏览
0

我希望我的工作表中的列更改为负数,因为此列表示“缺货” .

我从下面的链接获得了代码,它将给定范围的值更改为负值:

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

但问题是这需要用户的互动 .

Code:

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

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

Code:

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 ,而是想给它一个特定的范围 - 但是可以改变 .

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

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

也许范围可以基于 C4C144 中的文本 - 所以列 F 中这两个文本单元格之间的任何内容都是负数?

1 回答

  • 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
    

相关问题