Use of If statement with multiple conditions

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

提交回复
热议问题