【问题标题】:Change Numbers in Variable Range to Negative将可变范围内的数字更改为负数
【发布时间】:2018-10-16 09:50:51
【问题描述】:

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

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

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 列中这两个文本单元格之间的任何内容都是负数?

【问题讨论】:

  • 作为工作表更改事件的第一步,您必须检查您正在编辑的单元格是否在所需范围内
  • 不确定您的代码是如何工作的 - Target.Address 是一个字符串,而 WorkRng 是一个范围。 WorkRng 将引用包含数字的所有单元格,而 Target.Address 将是您更改的选择中的任何单元格的地址。

标签: excel vba


【解决方案1】:

我添加了大量的 cmets 来解释代码的作用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多