Use of If statement with multiple conditions

前端 未结 6 2021
挽巷
挽巷 2021-01-15 23:54

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

相关标签:
6条回答
  • 2021-01-16 00:17

    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 to True. To convince yourself of this, try Debug.Print CBool(13) and Debug.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

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

    use

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

    etc. Or, even better, a Select Case construct as suggested by @mwolfe02.

    0 讨论(0)
  • 2021-01-16 00:18

    Ok, So the problem here is the "If statement".

    The correct way of defining the 'OR' is as so

    If Sheet2.Range("B6").Value = 1 Or Sheet2.Range("B6").Value = 2 Or Sheet2.Range("B6").Value = 3 Then
    
    0 讨论(0)
  • 2021-01-16 00:29

    An alternative is to use Select..Case statements. I think it is a lot more readable for this kind of thing:

    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) = "rffffdffffdr"
        End With
    Case .... 
        ....   
    Case Else
        ....
    End Select
    
    0 讨论(0)
  • 2021-01-16 00:36

    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 with ElseIf. By using this to test your condition, you will always execute the first ElseIf condition resolving to True 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 same Case 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.

    0 讨论(0)
  • 2021-01-16 00:39
     If Sheet2.Range("B6").Value = 1 Or 2 Or 3 Then
    

    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)

    0 讨论(0)
  • 2021-01-16 00:41

    Lots of redundant code you can get rid of and as mentioned a couple of times go with select case.

    Try:

    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) = "rffffdffffdr"
        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
    
    0 讨论(0)
提交回复
热议问题