Cell-colouring macro

daba

New Member
Joined
Sep 2, 2005
Messages
13
.... I have 3 cells (vertical), that contain R,G,B values.

These "boxes" of the 3 cells is repeated 255 times, making 256 * 3 cells in total.

I want all 3 cells in each "box" to be coloured to those RGB values, whenever any one of them is changed.

I'm having a mental block at the moment, and don't know how to proceed.

I'm sure it must be striaght-forward, if someone could point me in the right direction.....
 
OK, so this manually setting the RGB values of the 3 vertical cells is getting tedious to the extreme, so am once again looking for a solution, macro or vba.

I think I can simplify things if I select the topmost of the 3 cells, and have a button ..
I would forget the button, and just double-click the top cell of the 3. This code would still need to go in the sheet's module.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Cancel = True
  With Target.Resize(3)
    .Interior.Color = RGB(.Cells(1).Value, .Cells(2).Value, .Cells(3).Value)
  End With
End Sub
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I would forget the button, and just double-click the top cell of the 3. This code would still need to go in the sheet's module.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Cancel = True
  With Target.Resize(3)
    .Interior.Color = RGB(.Cells(1).Value, .Cells(2).Value, .Cells(3).Value)
  End With
End Sub

Absolutely brilliant Peter, it does exactly what I want !!

Thank-you.....
 
Upvote 0
hi daba

If I've interpreted your query correctly this should work. If it doesn't, try and understand the code as both Peter and I have given you the core requirements; if you have any queries about particular elements feel free to ask. The challenge in both solutions is calculating which cells to colour.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim nShft As Integer 'used to align the groups of cells
    Dim nRow As Integer  'used to hold target row (just to make code easier to read)
    Dim nCol As Integer  'used to hold target column (just to make code easier to read)
    Const rSt = 23 'first data row
    
    nRow = Target.Row
    nCol = Target.Column
    
    If nRow >= 23 And nRow <= 70 And (nCol Mod 2) = 0 Then
        nShft = 1 - ((nRow - rSt) Mod 3)
    
    
        Range(Cells(nRow - 1 + nShft, nCol), Cells(nRow + 1 + nShft, nCol)).Interior.Color = _
                            RGB(Target.Offset(-1 + nShft), Target.Offset(nShft), Target.Offset(1 + nShft))
    End If
End Sub

Hope this helps.

regards
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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