Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cell background color to its containing RGB values. How?

Tags:

excel

vba

Below is a screenshot of what I hope to achieve in a way other than manually copying and pasting.

It's the material design color pallet and it looks great in Excel.

How can I loop through range B2:B15 and set each cell background color to its corresponding containing color?

Perhaps a VBA loop to go through the vertical B range of cells, parsing each cell content, and setting the cell background-color to the RGB value that the cell contains.

So a RED-50 cell containing rgb(255, 235, 238) becomes...

Range("B2").Interior.Color = RGB(255, 235, 238)

image1


UPDATE!

Thank you everyone for the help. Below is the google drive link to the Excel material design palette you've help me build.

Press Control + M to activate.

material.design.colors.xlsm

So ALL COLORS become themselves.

image2

like image 384
suchislife Avatar asked Nov 01 '25 05:11

suchislife


1 Answers

Perhaps something like that? I couldn't pass the cell's value into Interior.Color so I decided to use Split to extract each color individually. Adjust Sheet1 if needed.

Sub InteriorColor()
    Dim rngCell As Excel.Range
    Dim varColors As Variant
    Dim lngR As Long, lngG As Long, lngB As Long

    For Each rngCell In Sheet1.Range("B2:B15")
        varColors = Split(rngCell.Value, ",")
        lngR = Right(varColors(0), Len(varColors(0)) - 4)
        lngG = Trim(varColors(1))
        lngB = Left(varColors(2), Len(varColors(2)) - 1)

        rngCell.Interior.Color = RGB(lngR, lngG, lngB)
    Next rngCell
End Sub

Result:

enter image description here

like image 124
Justyna MK Avatar answered Nov 02 '25 19:11

Justyna MK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!