如何匹配多个条件并获取消息

时间:2019-05-24 12:57:58

标签: excel vba

在Excel VBA中,我想在3列中匹配3个条件,如果匹配则得到一条消息。

到目前为止,我的代码是:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A1:A10").Value = "America" And Range("B1:B10").Value = "cloudy" 
      And Range("C1:C10").Value > 30 Then
        MsgBox "This is the promised land!"
    End If
End Sub

我收到一个错误消息,说明存在不同的值。

Worksheet

2 个答案:

答案 0 :(得分:2)

一种不同的方法,但工作原理相似。

Dim i As Integer
For i = 2 To 10 ' Put something more variable instead of 10
   If Range("A" & i).Value = "Value4" And Range("B" & i).Value = "Value8" And Range("C" & i).Value > 30 Then
      MsgBox "Test"
   End If
Next i

Example Image

答案 1 :(得分:1)

在每一行上循环:

Sub ceckit()
    Dim cell As Range, A As Range
    Set A = Range("A1:A10")
    For Each cell In A
        With cell
            If .Value = "America" And .Offset(0, 1).Value = "cloudy" And .Offset(0, 2).Value > 30 Then
                MsgBox "This is the promised land!"
            End If
        End With
    Next cell
End Sub