值更改警报 VBA

Value change Alert VBA

所以我正在尝试发出警报,如果列更改为“496”,它会给出一个带有警报的 MsgBox,并且警报需要说明发生更改的行以及该值已更改为“496”,我还打算将单元格设置为红色(填充)。

我还是没有插入MsgBox,这个代码是朋友给我的,帮我在Excel的一个数据库里。而我在excel vba 的技能仍然很差,对此感到抱歉。但我正在学习。

Option Explicit

Private prevValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range

If Not Intersect(Target, Range("G3:G500")) Is Nothing Then


For Each cell In Target
Me.Cells(cell.Row, "496").Interior.ColorIndex = xlColorIndexNone
If cell.Value <> "" And cell.Value <> prevValue Then
Me.Cells(cell.Row, "496").Interior.ColorIndex = 3
End If
Next cell
End If

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
prevValue = Target.Value
End Sub

您的业务逻辑有点不清楚,代码段有错误。因此,与主要问题(通过消息框添加警报)相关的代码可能如下所示:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If Not Intersect(Target, Range("G3:G500")) Is Nothing Then
    For Each cell In Target 'need clarification
        'Me.Cells(cell.Row, "496").Interior.ColorIndex = xlColorIndexNone
        'If cell.Value <> "" And cell.Value <> prevValue Then
            'Me.Cells(cell.Row, "496").Interior.ColorIndex = 3
        'End If
       If cell.Value = "496" Then
            cell.Interior.ColorIndex = 3
            MsgBox ("Cell Row=" & cell.Row)
        Else
            cell.Interior.ColorIndex = xlColorIndexNone
        End If
    Next cell
End If
End Sub

如果 Range("G3:G500") 中的任何单元格值设置为“496”,它将设置 ColorIndex = 3 并显示一个显示单元格行号的消息框。您可以进一步修改提示信息:MsgBox ("Cell Row=" & cell.Row)与您的业务逻辑相关。

希望对您有所帮助。