值更改警报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)

希望它可以帮助。