我发布了一条关于"Alex Bell"已经更改的这段代码的问题,他帮我编写了一个MsgBox,每次出现值" 496"出现在该特定范围内。但是由于我对这门语言知识匮乏,我做了很多事情。
我试图实现的下一步是做同样的事情已经完成,如果值为" 496"则为MsgBox提醒,但现在用" 800"太。
那么问题是什么?问题是我无法想办法让这两个条件一起工作,例如它告诉我" 496"然后" 800"并填充包含该特定值的两个单元格。
这可能是一个容易解决的问题,但我再次成为vba的新手,当我在学校学习vb时,我们并没有学到那么多东西。因此,我希望有更多关于vba相关主题的问题,而我现在正在尝试学习它。
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 = 43
MsgBox ("The row where the status is 496 is located in: " & cell.Row)
Else
cell.Interior.ColorIndex = xlColorIndexNone
End If
Next cell
End If
'If Not Intersect(Target, Range("G3:G500")) Is Nothing Then
' For Each cell In Target
'
' If cell.Value = "800" Then
' cell.Interior.ColorIndex = 4
' MsgBox ("The row where the status is 800 is located in: " & cell.Row)
' Else
' cell.Interior.ColorIndex = xlColorIndexNone
' End If
' Next cell
'End If
End Sub
答案 0 :(得分:2)
If cell.Value = "496" Or cell.Value = "800" Then
cell.Interior.ColorIndex = 43
MsgBox ("The row where the status is 496 or 800 is located in: " & cell.Row)
Else
cell.Interior.ColorIndex = xlColorIndexNone
End If
或者像这样:
If cell.Value = "496" Then
cell.Interior.ColorIndex = 43
MsgBox ("The row where the status is 496 is located in: " & cell.Row)
ElseIf cell.Value = "800" Then
cell.Interior.ColorIndex = 45
MsgBox ("The row where the status is 800 is located in: " & cell.Row)
Else
cell.Interior.ColorIndex = xlColorIndexNone
End If
如果您想要更多检查,可以考虑将行号存储到变量中,最后可以调用MsgBox:
Dim rowNumbers As String
rowNumbers = ""
If Not Intersect(Target, Range("G3:G500")) Is Nothing Then
For Each cell In Target
If cell.Value = "496" Then
cell.Interior.ColorIndex = 43
rowNumbers = rowNumbers & cell.Row & " "
ElseIf cell.Value = "800" Then
cell.Interior.ColorIndex = 45
rowNumbers = rowNumbers & cell.Row & " "
Else
cell.Interior.ColorIndex = xlColorIndexNone
End If
Next cell
MsgBox ("The rows where the status is 496 or 800 is located in: " & rowNumbers)
End If