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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Oscar Cronquist

Board Regular
Joined
Apr 20, 2018
Messages
200
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
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,717
Office Version
  1. 365
Platform
  1. Windows
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
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,060
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,066
Messages
5,851,886
Members
431,471
Latest member
netyasser

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
Top