避免依赖事件相互触发

时间:2019-07-18 10:04:33

标签: excel vba

我有一个Worksheet_change,其中检查了两个事件(对C列的单元格进行编辑,对D列的单元格进行编辑)。问题在于,对C列的单元格进行修改会修改D列的单元格的值(反之亦然),因此Worksheet_change会反复触发,并且excel最终会崩溃。

如何避免出现问题,但要保持功能正常?

    Private Sub Worksheet_Change(ByVal Target As Range)



Dim rgFound As Range
Dim defVal As Range
Dim currParam As Range
Dim currParamDict As Range

Set targ = Intersect(Target, Range("A:A"))
If Not targ Is Nothing Then


With Worksheets("FT_CASE_xx")
    For Each defVal In .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Offset(, 1)

            Set currParam = defVal.Offset(, -1)

            Dim xlFirstChar As String
            xlFirstChar = Left$(currParam, 1)


            If xlFirstChar = "B" Then

                Set rgFound = Worksheets("DEF_BOOLEAN").Range("A:A").Find(currParam.value)
                defVal.Offset(, 1).Interior.Color = RGB(230, 230, 230)
                defVal.Offset(, 1).Locked = True

                defVal.Offset(, 2).Select

                With Selection.Validation
                                .Delete
                                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="TRUE,FALSE"
                                .IgnoreBlank = True
                                .InCellDropdown = True
                                .InputTitle = ""
                                .ErrorTitle = ""
                                .InputMessage = ""
                                .ErrorMessage = ""
                                .ShowInput = True
                                .ShowError = True
                End With
            Else
                Set rgFound = Worksheets("DEF_FLOAT").Range("A:A").Find(currParam.value)
                defVal.Offset(, 1).Interior.ColorIndex = 0
                defVal.Offset(, 1).Locked = False
                defVal.Offset(, 2).Locked = False

                defVal.Offset(, 1).NumberFormat = "0.000"
                defVal.Offset(, 2).NumberFormat = "0.000"
                defVal.Offset(, 3).NumberFormat = "0.000"
            End If

            If rgFound Is Nothing Then
                Debug.Print "Name was not found."
            Else

            If xlFirstChar = "B" Then
                Set currParamDict = rgFound.Offset(, 3)
            Else
               Set currParamDict = rgFound.Offset(, 5)
            End If

            defVal.value = currParamDict.value

            End If

    Next defVal
End With

Else
    Set targ = Intersect(Target, Range("C:C"))
    If Not targ Is Nothing Then

        Dim coeffVal As Range
        Dim currVal As Range
        Dim RequestedVal As Range

        With Worksheets("FT_CASE_xx")
            For Each coeffVal In .Range("C2", .Range("C" & Rows.Count).End(xlUp))

                    Set currVal = coeffVal.Offset(, -1)
                    Set RequestedVal = coeffVal.Offset(, 1)
                    Set ParamName = coeffVal.Offset(, -2)

                    Dim xlFirstChar2 As String
                    xlFirstChar2 = Left$(ParamName, 1)

                    If ((xlFirstChar2 = "F") And (IsEmpty(coeffVal.value) = False)) Then
                       RequestedVal.value = coeffVal.value * currVal.value
                    End If

            Next coeffVal
        End With

    Else

            Set targ = Intersect(Target, Range("D:D"))
            If Not targ Is Nothing Then

            Dim coeffsVal As Range
            Dim val As Range
            Dim reqVal As Range
            Dim Parameter As Range

            With Worksheets("FT_CASE_xx")
                For Each reqVal In .Range("D2", .Range("D" & Rows.Count).End(xlUp))

                        Set coeffsVal = reqVal.Offset(, -1)
                        Set val = reqVal.Offset(, -2)
                        Set Parameter = reqVal.Offset(, -3)

                        Dim xlFirstChar3 As String
                         xlFirstChar3 = Left$(Parameter, 1)

                        If ((xlFirstChar3 = "F") And (IsEmpty(reqVal.value) = False)) Then
                            If val.value = 0 Then
                                coeffsVal.value = reqVal.value
                            Else
                                coeffsVal.value = reqVal.value / val.value
                            End If
                        End If

              Next reqVal
            End With

    Else
                Exit Sub
    End If
    End If
End If

End Sub

也许对目标交叉路口的管理不同?怎么样?

1 个答案:

答案 0 :(得分:1)

我喜欢的方法(在其他情况下也可能有用)是在全局或模块级别(根据需要)创建变量,然后在每次代码运行时进行检查

Private disableEvents as Boolean

Private Sub Worksheet_Change(ByVal Target As Range)
    If disableEvents Then Exit Sub
    disableEvents=True

    <code here>

    disableEvents=False
End sub