Can this code be shortened?

SAT-65

Board Regular
Joined
Feb 10, 2008
Messages
69
I have this code that changes the background color of a text box based on the number in a cell. It works but it's long. I have 40 text boxes on the sheet that need to run this code as well. Any ideas?

Sub change_box1_color()
'
' change_box1_color Macro
'
'

If Range("F14").Value = 1 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 255)
Else
If Range("F14").Value = 2 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(51, 51, 255)
Else
If Range("F14").Value = 3 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
If Range("F14").Value = 4 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(51, 204, 51)
Else
If Range("F14").Value = 5 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 204)
Else
If Range("F14").Value = 6 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 255)
Else
If Range("F14").Value = 7 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 0)
Else
If Range("F14").Value = 8 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 192, 0)
Else
If Range("F14").Value = 9 Then
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(0, 0, 0)
Else
ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 255)
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Select Case would be better for this

Code:
Sub change_box1_color()
'
' change_box1_color Macro
'
'

Select Case Range("F14").Value
    Case 1: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 255)
    Case 2: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(51, 51, 255)
    Case 3: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 0, 0)
    Case 4: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(51, 204, 51)
    Case 5: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 204)
    Case 6: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 255)
    Case 7: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 0)
    Case 8: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 192, 0)
    Case 9: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(0, 0, 0)
    Case Else: ActiveSheet.Shapes("TextBox 259").Fill.ForeColor.RGB = RGB(255, 255, 255)
End Select
End Sub
 
Upvote 0
…taken a little further (untested):
Code:
Sub change_box1_color()
With ActiveSheet.Shapes("TextBox 259").Fill.ForeColor
    Select Case Range("F14").Value
        Case 2: .RGB = RGB(51, 51, 255)
        Case 3: .RGB = RGB(255, 0, 0)
        Case 4: .RGB = RGB(51, 204, 51)
        Case 5: .RGB = RGB(255, 255, 204)
        Case 7: .RGB = RGB(255, 255, 0)
        Case 8: .RGB = RGB(255, 192, 0)
        Case 9: .RGB = RGB(0, 0, 0)
        Case Else: .RGB = RGB(255, 255, 255)
    End Select
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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