使用具有多个条件的If语句 [英] Use of If statement with multiple conditions
问题描述
我已经编写了以下代码,基本上应该相应地将颜色设置为一些框。每当我运行此代码时,它运行第一种情况,即使需要选择其他情况。这是代码。
I have written the following code which is basically supposed colour some boxes accordingly. Whenever i run this code, it runs the first case i.e. even when some other case is required to be selected. here is the code.
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
我们将不胜感激。
推荐答案
If Sheet2.Range("B6").Value = 1 Or 2 Or 3 Then
这条线没有做你认为它正在做的事情。您需要输入如果Sheet2.Range(B6)。Value = 1或Sheet2.Range(B6)。Value = 2或Sheet2.Range(B6)。Value = 3 Or Sheet2.Range(B6)。值= 4然后
(或用替换中间变量Sheet2.Range(B6)。值
)
This line is not doing what you think it is doing. You need to put 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
(or substitute an intermediate variable in for Sheet2.Range("B6").Value
)
这篇关于使用具有多个条件的If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!