我为此搜索了一个低点,但无法让它在我的整个范围内工作。这是我的第一篇文章,所以要温柔。
我有一个算法可以分析某些产品从一个生产阶段转移到下一个生产阶段的时间。然后,它提供完成下一阶段生产所需的资源量。
我的问题是,当资源超出可用资源时,我想要一个弹出框出现并警告用户;这应该发生在范围内的所有细胞。我已经成功地工作了,但只有当单元格“S7”超过该值时。任何帮助将不胜感激。
代码如下:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range
Set myRng = ThisWorkbook.Sheets("SMT 2").Range("S7:S26")
For Each mycell In myRng
If mycell.Value >= 16 Then sVar = _
MsgBox("Will Enough Pre-Wave Resources be Available?", 4, "Attention!")
If sVar = 7 Then
Application.Undo
End If
Exit For
Next
End Sub
答案 0 :(得分:2)
您不需要循环。您可以使用Intersect
检查Range("A7:A26")
范围内的任何单元格是否已更改。另请注意,以下代码无法满足用户在该范围内粘贴值>16
的情况。
这是你在尝试的吗?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range
Dim lRow As Long
If Target.CountLarge > 1 Then Exit Sub
On Error GoTo Whoa
Set myRng = Range("A7:A26")
Application.EnableEvents = False
If Not Intersect(Target, myRng) Is Nothing Then
lRow = Target.Row
If Range("S" & lRow).Value >= 16 Then sVar = _
MsgBox("Will Enough Pre-Wave Resources be Available?", 4, "Attention!")
If sVar = 7 Then Application.Undo
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
答案 1 :(得分:1)
您想要浏览所有单元格还是S7到S26?
如果你想要通过S7:S26,这就是代码:(退出For是在错误的地方)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range
Set myRng = ThisWorkbook.Sheets("SMT 2").Range("S7:S26")
For Each mycell In myRng
If mycell.Value >= 16 Then
sVar = MsgBox("Will Enough Pre-Wave Resources be Available?", 4, "Attention!")
If sVar = 7 Then
Application.Undo
Exit For
End If
End if
Next
End Sub