在特定列的单元格更改时显示消息

我是新的Excel VBA。 当K列中的任何单元格更改时,我想显示一条警告消息。
我写了这个代码:

 Private Sub Worksheet_Change(ByVal Target As Range) Dim row As Integer Dim lrow As Long With ActiveSheet lrow = .Range("K" & .Rows.Count).End(xlUp).row For satir = 5 To lrow If Cells(row, 11).Value > 400 And Cells(row, 12).Value = "" Then MsgBox _ ("Risk Point of your operation is still high, Please Identify Contingency Plan") End If Next row For row = 5 To lrow If Cells(row, 22).Value > 200 And Cells(row, 24).Value = "" Then MsgBox ("Risk Point is very high, Please Identify your Mitigation Plan") End If Next row End With End Sub 

此代码正在工作,但显示在工作表中所做的所有更改的警告消息。

写入您的Worksheet_Change小组:

  If Target.Column = 11 Then MsgBox "warning" End If 

这会在用户更改k列中的值时立即发送“警告”

这是重构你的代码。 C. Henke的这个答案已经回答了你的问题。

 Dim row As Long '~~> use long to avoid overflow Dim lrow As Long, satir As Long With Me '~~> to refer to the worksheet itself, you can use Me object If Not Intersect(.Range("K:K"), Target) Is Nothing Then '~~> check if target cell reside in K column lrow = .Range("K" & .Rows.Count).End(xlUp).row For satir = 5 To lrow If .Cells(row, 11).Value > 400 _ And .Cells(row, 12).Value = "" Then MsgBox "Risk Point of your operation is still high." & _ vbNewLine & "Please Identify Contingency Plan" End If Next satir For row = 5 To lrow If .Cells(row, 22).Value > 200 _ And .Cells(row, 24).Value = "" Then MsgBox "Risk Point of your operation is still high." & _ vbNewLine & "Please Identify Contingency Plan" End If Next row End If End With 

希望这会让你走。