VBA停止单元格计算

8

我对Excel中的VBA非常陌生,但被要求在单元格发生更改时进行验证,遇到了一些困难。

因此,用户需要在单元格(例如D16)中输入货币值,因此我想利用工作表上的_Change事件,这个方法效果很好。

然而,当在D16中提交了一个条目时,我需要让工作表的其他部分不完成计算,也就是说,当输入500000时,其他单元格将从另一个工作表中更新值。

我的代码:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target = Range("D16") Then

       Dim numeric
       numeric = IsNumeric(Target)


       If numeric = False Then

          MsgBox "error"
          Exit Sub

          /// this is where I need to "stop" the calculations from firing

       End If

    End If

End Sub

@Santosh,是的,这两个都没有解决我们的问题。最终我们连接了每个可用的事件,并在其中调用了Worksheet_Change事件。奇怪的情况,我知道,我无法解释为什么,但它起作用了。 - JadedEric
2个回答

21

我希望以下代码能够帮助到您。您需要将其粘贴到工作表代码区域中。

Private Sub Worksheet_Change(ByVal Target As Range)

    On Error Resume Next

    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    Dim rng As Range
    Set rng = Range("D16")

    If Not Intersect(Target, rng) Is Nothing And IsNumeric(Target) Then

        If Target.Value >= 500000 Then
            MsgBox "Value is greater than 500000"
        End If


    End If


    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
End Sub

11

使用 Application.Calculation = xlCalculationManual

别忘了将其再次切换回来:Application.Calculation = xlCalculationAutomatic


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接