Help on shorter way to write IF statement

SPLUCENA

Board Regular
Joined
Feb 24, 2009
Messages
189
Hi All,

I write this code, does the job but seems long. Is there a shorter way to rewrite this code and still do its job?
Code:
            If ActiveSheet.Range("B2").Value = "Red" Then
                ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 0, 0)
            Else
            If ActiveSheet.Range("B2").Value = "Green" Then
                ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(0, 255, 0)
            Else
            If ActiveSheet.Range("B2").Value = "Yellow" Then
                ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 255, 0)
            Else
            If ActiveSheet.Range("B2").Value = "Orange" Then
                ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 69, 0)
            End If
            End If
            End If
            End If
           
            If ActiveSheet.Range("B3").Value = "Red" Then
                ActiveSheet.Shapes("Logged").Fill.ForeColor.RGB = RGB(255, 0, 0)
            Else
            If ActiveSheet.Range("B3").Value = "Green" Then
                ActiveSheet.Shapes("Logged").Fill.ForeColor.RGB = RGB(0, 255, 0)
            Else
            If ActiveSheet.Range("B3").Value = "Yellow" Then
                ActiveSheet.Shapes("Logged").Fill.ForeColor.RGB = RGB(255, 255, 0)
            Else
            If ActiveSheet.Range("B3").Value = "Orange" Then
                ActiveSheet.Shapes("Logged").Fill.ForeColor.RGB = RGB(255, 69, 0)
            End If
            End If
            End If
            End If

Thanks for your help.

Rgds,

splucena
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
Select Case ActiveSheet.Range("B2").Value
    Case Is = "Red"
        ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 0, 0)
    Case Is = "Green"
       ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(0, 255, 0)
    Case Is = "Yellow"
       ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 255, 0)
   Case Is = "Orange"
       ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 69, 0)
End Select
Select Case ActiveSheet.Range("B3").Value
    Case Is = "Red"
        ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 0, 0)
    Case Is = "Green"
       ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(0, 255, 0)
    Case Is = "Yellow"
       ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 255, 0)
   Case Is = "Orange"
       ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 69, 0)
End Select
 
Upvote 0
This is a possible:

Code:
Dim arrRng, arrShp, i As Long

arrRng = Range("B2:B3")
arrShp = Array("shEAC", "Logged")

For i = LBound(arrRng, 1) To UBound(arrRng, 1)
    Select Case arrRng(i, 1)
        Case "Red"
            ActiveSheet.Shapes(arrShp(i - 1)).Fill.ForeColor.RGB = RGB(255, 0, 0)
        Case "Green"
            ActiveSheet.Shapes(arrShp(i - 1)).Fill.ForeColor.RGB = RGB(0, 255, 0)
        Case "Yellow"
            ActiveSheet.Shapes(arrShp(i - 1)).Fill.ForeColor.RGB = RGB(255, 255, 0)
        Case "Orange"
            ActiveSheet.Shapes(arrShp(i - 1)).Fill.ForeColor.RGB = RGB(255, 69, 0)
    End Select
Next
 
Upvote 0
This is a little shorter:
Code:
            If ActiveSheet.Range("B2").Value = "Red" Then
                ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 0, 0)
            ElseIf ActiveSheet.Range("B2").Value = "Green" Then
                ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(0, 255, 0)
            ElseIf ActiveSheet.Range("B2").Value = "Yellow" Then
                ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 255, 0)
            ElseIf ActiveSheet.Range("B2").Value = "Orange" Then
                ActiveSheet.Shapes("shEAC").Fill.ForeColor.RGB = RGB(255, 69, 0)
            End If
           
            If ActiveSheet.Range("B3").Value = "Red" Then
                ActiveSheet.Shapes("Logged").Fill.ForeColor.RGB = RGB(255, 0, 0)
            ElseIf ActiveSheet.Range("B3").Value = "Green" Then
                ActiveSheet.Shapes("Logged").Fill.ForeColor.RGB = RGB(0, 255, 0)
            ElseIf ActiveSheet.Range("B3").Value = "Yellow" Then
                ActiveSheet.Shapes("Logged").Fill.ForeColor.RGB = RGB(255, 255, 0)
            ElseIf ActiveSheet.Range("B3").Value = "Orange" Then
                ActiveSheet.Shapes("Logged").Fill.ForeColor.RGB = RGB(255, 69, 0)
            End If
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top