简体   繁体   English

Excel VBA-值和MsgBox

[英]Excel VBA - Value & MsgBox

I posted a question about this piece of code that is already changed by "Alex Bell" , He helped me with the code making a MsgBox appear every time the value "496" appears in that specific range. 我发布了一个有关此代码段的问题,该代码段已由“ Alex Bell”更改,他帮助我编写了使MsgBox出现的代码,每次值“ 496”出现在该特定范围内。 But due to my poor knowledge in this language, there's a lot of things I cannot do. 但是由于我对这种语言的了解不足,所以我无法做很多事情。

the next step I was trying to achieve was doing the same thing that is already done, the MsgBox alert if the value is "496", but now with "800" too. 我尝试实现的下一步是执行已经完成的相同操作,如果值是“ 496”,则MsgBox警报,但现在也显示“ 800”。

So what is the problem? 那是什么问题呢? The Problem is that I cannot figure a way to put the two conditions to work together, for example it tells me where is the "496" and then the "800" and fills both of the cells that contain that specific values. 问题是我无法找到一种方法将两个条件放在一起工作,例如,它告诉我“ 496”和“ 800”在哪里,并填充包含该特定值的两个单元格。

It's probably a easy question to solve, but again I'm a newbie to vba and when I studied vb in school we didn't learn that much. 这可能是一个很容易解决的问题,但是我还是vba的新手,当我在学校学习vb时,我们学到的东西不多。 So be expecting more questions from me on topics related to vba and I'm trying to learn it at the moment. 因此,期待我提出更多与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

Or like this: 或像这样:

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

If you would like to have more checks, you can consider to store the row numbers to print into a variable and at the very end you can call the MsgBox: 如果您想进行更多检查,可以考虑将要打印的行号存储到变量中,最后可以调用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM