worksheet_calculate()不起作用

时间:2016-05-09 13:44:36

标签: excel vba excel-vba

我在名为Main的工作表中添加了一个私有子工作表_calculate()。我在列AP中有一个值,其中包含从其他工作表派生的公式,如果该数字大于X中的值,我想显示一条消息作为警告它已结束,但代码没有工作任何建议为什么?

Private Sub Worksheet_Calculate()
If Sheets("Main").Range("AP7").value > Sheets("Main").Range("x7").value Then
    MsgBox "You Are Over Pieces Suggested"
End If
End Sub

2 个答案:

答案 0 :(得分:1)

试试这个。

Private Sub Worksheet_Calculate()
If Range("AP7").Value > Range("X7").Value Then
MsgBox "You Are Over Pieces Suggested."
End If
End Sub

EDITED #### 编辑原始代码以作为Worksheet_Calculate运行而不是更改。 正在尝试为您设置范围到列。

EDIT #########

我喜欢挑战。试试这个。

Private Sub Worksheet_Calculate()

Set Target = Range("AP:AP").SpecialCells(xlCellTypeFormulas)
If Target Is Nothing Then Exit Sub
    For Each c In Target
        If c > Range("X" & c.Row) Then
            MsgBox "You Are Over Pieces Suggested - Cell " & "AP" & c.Row
        End If
    Next

End Sub

答案 1 :(得分:0)

考虑使用“自定义”公式在单元格AP7上使用数据验证:= AP7< = $ X $ 7 填写验证菜单上的错误警报选项卡:停止; “你是建议的”。我认为这可能会实现你想要的没有任何宏。实际上,它可以防止首先输入无效的数字。

相关问题