Excel VBA - 值和消息框
Excel VBA - Value & MsgBox
我发布了关于这段代码的问题,该代码已被 "Alex Bell" 更改,他帮助我编写了每次值“496”出现在该特定范围内时都会出现 MsgBox 的代码。但是由于我对这门语言的了解不多,所以有很多事情我做不到。
我试图实现的下一步是做已经完成的同样的事情,如果值为“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
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
我发布了关于这段代码的问题,该代码已被 "Alex Bell" 更改,他帮助我编写了每次值“496”出现在该特定范围内时都会出现 MsgBox 的代码。但是由于我对这门语言的了解不多,所以有很多事情我做不到。
我试图实现的下一步是做已经完成的同样的事情,如果值为“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
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