使用具有多个条件的 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
我们将不胜感激您的帮助。
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
Your help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另一种方法是使用
Select..Case
语句。我认为对于这种事情来说它更具可读性:An alternative is to use
Select..Case
statements. I think it is a lot more readable for this kind of thing:这条线没有做你认为它正在做的事情。您需要输入 If Sheet2.Range("B6").Value = 1 Or Sheet2.Range("B6").Value = 2 Or Sheet2.Range("B6").Value = 3 Or Sheet2.Range ("B6").Value = 4 然后(或用中间变量替换
Sheet2.Range("B6").Value
)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 forSheet2.Range("B6").Value
)问题的答案在于,
Or
条件中的数字被隐式强制为布尔值,并且当发生这种情况时,除 0 之外的所有内容都被强制为True
。要说服自己相信这一点,请尝试Debug.Print CBool(13)
和Debug.Print CBool(0)
。我有点恼火的是,之前发布答案的人都没有解释这一点,因此这篇文章可能会被认为是重复的!
而不是
使用
等。或者,更好的是,使用 @mwolfe02 建议的
Select Case
构造。The answer to your problem lies in the fact that the numbers in your
Or
conditions are implicitly coerced to Boolean values, and that when this happens, everything except 0 is coerced toTrue
. To convince yourself of this, tryDebug.Print CBool(13)
andDebug.Print CBool(0)
.I am a bit peeved that none of the people who have posted previous answers have explained this, hence this post which might otherwise have been considered repetitive!
Instead of
use
etc. Or, even better, a
Select Case
construct as suggested by @mwolfe02.好的,
所以这里的问题是“If语句”。
定义“OR”的正确方法如下
Ok,
So the problem here is the "If statement".
The correct way of defining the 'OR' is as so
除了此处发布的其他答案指出的错误之外,重要的是要注意用于测试条件的构造是
If
和ElseIf
。通过使用它来测试您的条件,您将始终执行解析为True
的第一个ElseIf
条件,并跳过可能定义的任何后续条件。这意味着根据您想要格式化的适当条件,您最终可能会在格式化时遇到逻辑问题。
因此,我建议使用上述响应中 @mwolfe02 提供的
Case
语句构造以及同一Case
语句中的所有类似格式条件。这将防止根据特定数据排序中满足的条件而发生各种格式化情况。希望有帮助。
In addition to the errors noted by the other answers posted here, it is important to note the construct used for testing your condition is
If
withElseIf
. By using this to test your condition, you will always execute the firstElseIf
condition resolving toTrue
and skip any later conditions which may be defined.This means you may end up with logical issues in formatting according to the appropriate conditions you intended to format.
For this reason, I would recommend using the
Case
statement construct as provided by @mwolfe02 in the response above along with all similar formatting conditions within the sameCase
statement. This would prevent various formatting situations from occurring based on what conditions had been met in a particular ordering of data.Hope that helps.
您可以删除许多冗余代码,并且正如多次提到的,选择案例。
尝试:
Lots of redundant code you can get rid of and as mentioned a couple of times go with select case.
Try: