使用具有多个条件的If语句

时间:2011-06-10 12:10:52

标签: vba if-statement

我已经编写了以下代码,基本上应该相应地将颜色设置为一些框。每当我运行这个代码时,它就会运行第一种情况,即使需要选择其他情况。这是代码。

Sub Macro_quaterly()
If Sheet2.Range("B6").Value = 1 Or 2 Or 3 Then
    Range("D7").Select
    With Selection.Interior
        '.Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
        Sheet2.Cells(6, 11) = "rrrrrrr"
    End With
ElseIf Sheet2.Range("B6").Value = 4 Or 5 Or 6 Or 7 Then
    Range("D7:E7").Select
    With Selection.Interior
        '.Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
        Sheet2.Cells(6, 12) = "rddddddr"
    End With
ElseIf Sheet2.Cells(6, 2) = 8 Or 9 Or 10 Or 11 Then
 Range("D7:F7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 12 Or 13 Or 14 Or 15 Then
 Range("D7:G7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 16 Or 17 Or 18 Or 19 Then
 Range("D7:H7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 20 Or 21 Or 22 Or 23 Then
 Range("D7:I7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 24 Or 25 Or 26 Or 27 Then
 Range("D7:J7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 28 Or 29 Or 30 Or 31 Then
 Range("D7:K7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 32 Or 33 Or 34 Or 35 Then
 Range("D7:L7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 36 Or 37 Or 38 Or 39 Then
 Range("D7:M7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 40 Or 41 Or 42 Or 43 Then
 Range("D7:N7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 44 Or 45 Or 46 Or 47 Then
 Range("D7:O7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 48 Or 49 Or 50 Or 51 Then
 Range("D7:P7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 52 Or 53 Or 54 Or 55 Then
 Range("D7:Q7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 56 Or 57 Or 58 Or 59 Then
 Range("D7:R7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
ElseIf Sheet2.Cells(6, 2) = 60 Then
 Range("D7:S7").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End If

End Sub

我们将不胜感激。

6 个答案:

答案 0 :(得分:9)

另一种方法是使用Select..Case语句。我认为这种事情更具可读性:

Select Case Sheet2.Range("B6").Value 
Case 1, 2, 3
    Range("D7").Select
    With Selection.Interior
        '.Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
        Sheet2.Cells(6, 11) = "rrrrrrr"
    End With
Case 4, 5, 6, 7
    Range("D7:E7").Select
    With Selection.Interior
        '.Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
        Sheet2.Cells(6, 12) = "rddddddr"
    End With
Case .... 
    ....   
Case Else
    ....
End Select

答案 1 :(得分:5)

 If Sheet2.Range("B6").Value = 1 Or 2 Or 3 Then

这条线没有按照你的想法做。您需要将If Sheet2.Range("B6").Value = 1 Or Sheet2.Range("B6").Value = 2 Or Sheet2.Range("B6").Value = 3 Or Sheet2.Range("B6").Value = 4 Then(或替换中间变量替换为Sheet2.Range("B6").Value

答案 2 :(得分:4)

问题的答案在于Or条件中的数字被隐式强制转换为布尔值,并且当发生这种情况时,除0之外的所有数字都被强制转换为True。要说服自己,请尝试Debug.Print CBool(13)Debug.Print CBool(0)

我有点恼火的是,没有发布过前一个答案的人都解释过这个,因此这个帖子可能会被认为是重复的!

而不是

If Sheet2.Range("B6").Value = 1 Or 2 Or 3 Then

使用

If Sheet2.Range("B6").Value = 1 Or _
    Sheet2.Range("B6").Value = 2 Or _
    Sheet2.Range("B6").Value = 3 Then

等。或者,甚至更好,@ mwolfe02建议的Select Case构造。

答案 3 :(得分:1)

确定, 所以这里的问题是“如果声明”。

定义'OR'的正确方法是如此

If Sheet2.Range("B6").Value = 1 Or Sheet2.Range("B6").Value = 2 Or Sheet2.Range("B6").Value = 3 Then

答案 4 :(得分:1)

除了此处发布的其他答案中注明的错误之外,请务必注意用于测试您的条件的构造是IfElseIf。通过使用它来测试您的条件,您将始终执行解析为ElseIf的第一个True条件,并跳过可能定义的任何后续条件。

这意味着您可能会根据您要格式化的适当条件在格式化方面遇到逻辑问题。

出于这个原因,我建议在上面的响应中使用@ mwolfe02提供的Case语句结构以及同一Case语句中的所有类似格式条件。这将防止根据特定数据排序中满足的条件发生各种格式化情况。

希望有所帮助。

答案 5 :(得分:0)

你可以摆脱很多冗余的代码,并且如所提到的那样多次使用select case。

尝试:

Sub Macro_quaterly()
    Dim rCell As Range

    Select Case Sheet2.Range("B6").Value
    Case 1, 2, 3
        Set rCell = Range("D7")
        Sheet2.Cells(6, 11) = "rrrrrrr"
    Case 4, 5, 6, 7
        Set rCell = Range("D7:E7")
        Sheet2.Cells(6, 12) = "rddddddr"
    Case 8, 9, 10, 11
        Set rCell = Range("D7:F7")

    Case Else

    End Select

    With rCell.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

    Set rCell = Nothing

End Sub