所以我尝试做一个警告,如果列被更改为" 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
答案 0 :(得分:1)
您的业务逻辑有点不清楚,代码段包含错误。因此,与主要问题相关(通过Message Box添加警报)代码可能如下所示:
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
如果ColorIndex = 3
中的任何Cell值设置为&#34; 496&#34;它将设置Range("G3:G500")
并显示一个显示Cell的行号的消息框。您可以进一步修改与您的业务逻辑相关的提示消息:MsgBox ("Cell Row=" & cell.Row)
。
希望它可能会有所帮助。