当单元格达到确定的值时显示消息

这是我的MS Excel的VBA脚本

Sub Reached_150() Dim cella As Range Dim nomi As Range For Each cella In [p2:p10] For Each nomi In [a2:a10] If cella.Value = 150 Then MsgBox "Lo studente " & nomi & " ha terminato le ore." Next nomi Next cella End Sub 

在范围p2:p10中,计算总数,当单元格的总和达到150时,显示名称在A列的学生已完成小时的消息。

关于我有两个问题:1)第一个单元格到达150小时后,所有学生的名字都会出现消息,我该如何避免? 2)如何自动创build脚本,而不是手动? 等待答案感谢任何人可以帮助我

我认为最可能的情况是,你正试图赶上时间单元相关行的名称。

如果是这样,你的代码应该是:

 For Each cella In [p2:p10] If cella.Value = 150 Then MsgBox "Lo studente " & _ cella.Offset(,-15).Value & " ha terminato le ore." Next cella 

你没有提到如果值大于150,应该发生什么。如果你特别想要150,那么从下面的">=150"中删除>

要使其自动使用Worksheet_Change事件。 将其粘贴在“工作表代码”区域中。

 Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range, aCell As Range Set rng = Range("P2:P10") '~~> Check if there is any value >= 150 in that range If Application.WorksheetFunction.CountIf(rng, ">=150") Then For Each aCell In rng If aCell.Value >= 150 Then _ MsgBox "Lo studente " & _ Range("A" & aCell.Row).Value & _ " ha terminato le ore." Next aCell End If End Sub