Use of If statement with multiple conditions

前端 未结 6 2022
挽巷
挽巷 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: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
    

提交回复
热议问题