Vba 当特定列';s细胞变化

Vba 当特定列';s细胞变化,vba,excel,Vba,Excel,我是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

我是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

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

将此代码写入工作表\u Change Sub:

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

这将在用户更改k列中的值时立即发送“警告”,这是在重构代码。我已经回答了你的问题

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
希望这能让你走