2011-06-10 106 views
1

我已经写了下面的代码,它基本上是相应的颜色一些盒子。每当我运行这段代码时,它都会运行第一种情况,即使在需要选择其他一些情况时也是如此。这里是代码。使用if条件多条件

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 

您的帮助,将不胜感激。

+2

多德。你可以在[代码]标签中发布该代码,并且可以使用诸如换行符或两个代码之类的代码? – 2011-06-10 12:12:21

+0

哇!我希望能读到... – diagonalbatman 2011-06-10 12:13:47

+1

现在全部清理。 – jonsca 2011-06-10 12:18:47

回答

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

+0

我一直想看看是否支持if(foo ==(bar或baz或biz))......因为它似乎我一直在打字,最初...... – 2011-06-10 15:32:27

1

好的, 所以这里的问题是“If语句”。

定义正确的方法“或”是如此

If Sheet2.Range("B6").Value = 1 Or Sheet2.Range("B6").Value = 2 Or Sheet2.Range("B6").Value = 3 Then 
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 
+2

我可能会争辩说,是做这件事的最好方式 - 并阅读OP使用“Case”这个词组的问题 - 也许这是最初的意图? – diagonalbatman 2011-06-10 13:44:18

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结构。

1

除了这里发布的其他答案提到的错误之外,重要的是要注意用于测试您的病症的构造是IfElseIf。通过使用它来测试您的条件,您将始终执行解析为True的第一个ElseIf条件,并跳过可能定义的任何后续条件。

这意味着根据您打算格式化的适当条件,最终可能会出现逻辑问题。

因此,我建议使用上述响应中@ mwolfe02提供的Case语句结构以及相同Case语句中的所有类似格式条件。这将防止各种格式化情况发生的基础上,特定的数据顺序已满足什么条件。

希望有所帮助。

+0

谢谢大家.. :-) – mehak 2011-06-13 05:27:24

+0

嘿..theres在同一代码中面临的另一个问题..我猜它应该是简单的,但我真的是新的vba..could任何人都可以帮我请例如..我使用以下vba代码来简单地将现有单元格的值分配给另一个单元格中的另一个单元格......但它给出了运行时错误424..Kindly help..the代码如下所示 Sub Selection_cell_for_payoff_quaterly() 如果Sheet2.Cells( 6,2)。价值= 1然后 Sheet2.Cells(7,2)。价值= Sheet6.Range( “D7”)值 结束如果 结束子 – mehak 2011-06-13 05:47:21

+1

@mehak:请不要问新问题在评论中。只需发布一个新问题。 – 2011-06-14 19:31:37

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