Multiple conditional formats on a single Object in VBA
-
11-12-2019 - |
Question
I have an object (i.e. a ractangle shape holding text), i would like to have the shape change colour(status) dependant on the text within a cel(from a drop down list).
I am using 'If' functions to format the shape at present, and i feel this is the best way; however i am not sure how to have multiple 'IF' formulae running in VBA.
i have run a successful 'If' formula for formatting, but this was only able to change between 2 colours. Here is my single 'if' Formula.
If Range("I2") = "Deviation" Then
Worksheets("M2").Shapes("Rect1").Fill.ForeColor.RGB = RGB(79, 79, 79)
Else
Worksheets("M2").Shapes("Rect1").Fill.ForeColor.RGB = RGB(0, 102, 204)
End If
When i added other conditions/ 'If' formulae to the script it failed to change the object colour. The closest I got was with 'If' and 'end If' formulae to cover all posibilities. Here is my closest extraction (from my experience).
If Range("I2") = "Yes" Then
Worksheets("M2").Shapes("Rect1").Fill.ForeColor.RGB = RGB(0, 128, 0)
End If
If Range("I2") = "No" Then
Worksheets("M2").Shapes("Rect1").Fill.ForeColor.RGB = RGB(218, 9, 4)
End If
If Range("I2") = "In Progress" Then
Worksheets("M2").Shapes("Rect1").Fill.ForeColor.RGB = RGB(201, 129, 13)
End If
If Range("I2") = "Deviation" Then
Worksheets("M2").Shapes("Rect1").Fill.ForeColor.RGB = RGB(79, 79, 79)
Else
Worksheets("M2").Shapes("Rect1").Fill.ForeColor.RGB = RGB(0, 102, 204)
End If
Solution
To add to Alexandre's answer (don't forget to accept it!), you can refactor a little to avoid repetition:
Dim clr As Long
Select Case Range("I2").Value
Case "Yes": clr = RGB(0, 128, 0)
Case "No": clr = RGB(218, 9, 4)
Case "In Progress": clr = RGB(201, 129, 13)
Case "Deviation": clr = RGB(79, 79, 79)
Case Else: clr = RGB(0, 102, 204)
End Select
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = clr
OTHER TIPS
ElseIf would be an improvement, but the cleanest way is to just use select like this:
Select Case Range("I2").Value
Case "Yes"
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(0, 128, 0)
Case "No"
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(218, 9, 4)
Case "In Progress"
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(201, 129, 13)
Case "Deviation"
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(79, 79, 79)
Case Else
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(0, 102, 204)
End Select
This code has been tested to work on my computer.
Edit: Just for the sake of completeness, here is the same code using if/elseif/else:
If Range("I2") = "Yes" Then
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(0, 128, 0)
ElseIf Range("I2") = "No" Then
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(218, 9, 4)
ElseIf Range("I2") = "In Progress" Then
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(201, 129, 13)
ElseIf Range("I2") = "Deviation" Then
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(79, 79, 79)
Else
ActiveSheet.Shapes("Rect1").Fill.ForeColor.RGB = RGB(0, 102, 204)
End If