From colour code to RGB in this macro?

jberg123

New Member
Joined
Feb 24, 2011
Messages
45
How can I modify this code to use RGB(x,y,z) code instead? Thanks

Private Sub Worksheet_Calculate()
Dim icolor As Integer, c As Range
For Each c In Range("G21:G45,I21:I45,K21:K45")
Select Case c.Offset(, 1)
Case 1: icolor = 4
Case 2: icolor = 35
Case 3: icolor = 44
Case 4: icolor = 6
Case 5: icolor = 2
End Select
c.Interior.ColorIndex = icolor
Next c
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try

Code:
Private Sub Worksheet_Calculate()
Dim c As Range
For Each c In Range("G21:G45,I21:I45,K21:K45")
    Select Case c.Offset(, 1).Value
        Case 1: c.Interior.Color = RGB(0, 255, 0)
        Case 2: c.Interior.Color = RGB(204, 255, 204)
        Case 3: c.Interior.Color = RGB(255, 204, 0)
        Case 4: c.Interior.Color = RGB(255, 255, 0)
        Case 5: c.Interior.Color = RGB(255, 255, 255)
    End Select
    Next c
End Sub
 
Upvote 0
Try:-
Code:
With Range("F1")
    .Interior.Color = RGB(250, 250, 0)
    .Font.Color = RGB(0, 255, 0)
End With
Mick
 
Upvote 0
Try

Code:
Private Sub Worksheet_Calculate()
Dim c As Range
For Each c In Range("G21:G45,I21:I45,K21:K45")
    Select Case c.Offset(, 1).Value
        Case 1: c.Interior.Color = RGB(0, 255, 0)
        Case 2: c.Interior.Color = RGB(204, 255, 204)
        Case 3: c.Interior.Color = RGB(255, 204, 0)
        Case 4: c.Interior.Color = RGB(255, 255, 0)
        Case 5: c.Interior.Color = RGB(255, 255, 255)
    End Select
    Next c
End Sub


Thank you, worked brilliantly!

Another one, how do I add additional arguments to the cases?

E.g. I'd like to change font colour as well. I tried

...
Case 1: c.Interior.Color = RGB(0, 255, 0)
c.Font.Color = RGB(0,0,0)
Case 2: c.Interior.Color = RGB(204, 255, 204)
...

but it didn't work.

Many thanks
 
Upvote 0
Try like this

Code:
Case 1: c.Interior.Color = RGB(0, 255, 0): c.Font.Color = RGB(0, 0, 0)
 
Upvote 0
Or on multiple lines by removing the colon

Code:
Case 1
   c.Interior.Color = RGB(0, 255, 0)
   c.Font.Color = RGB(0, 0, 0)
 
Upvote 0
Code:
Private Sub Worksheet_Calculate()
  For Each c In Range("G21:G45,I21:I45,K21:K45")
    c.interior.color= RGB(Choose(c.Offset(, 1), 0, 204, 255, 255, 255), Choose(c.Offset(, 1), 255, 255, 204, 255, 255), Choose(c.Offset(, 1), 0, 204, 0, 0, 255))
  next
End Sub
But simpler seems:
Code:
Private Sub Worksheet_Calculate()
  For Each c In Range("G21:G45,I21:I45,K21:K45")
    c.interior.colorindex= Choose(c.Offset(, 1),4,35,44,6,2)
  next
End Sub
<!-- / message -->
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,815
Members
452,946
Latest member
JoseDavid

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