Excel+VBA How can I set the bg colour of a cell using its own contents? -
i'm trying sheet entering rgb value cell set background colour value. know can use
range("a1:be57").interior.color = rgb(127, 255, 127)
to set background whole range 1 colour, each individual cell in range pull own contents.
ideally having
range("a1:be57").interior.color = rgb(.value)
where .value replaced actual contents of cell (i.e. 127, 255, 127) each instance in range. though i'm aware unlikely simple.
is possible?
ok. here vba function takes string , attempts turn vba color-compatible string. below extremely simple usage example. should able take function , add code base. there no input validation ensure rgb string in correct format have tried original value of "127, 255, 127" , works "'127,255,127" value in a1. clear now.
' converts rgb string value hex string can use set color properties ' splits string rgb values, reverses them vba, , returns hex string public function convertvaluetocolour(rgbstring string) string ' define array hold rgb values dim colors() string ' initialize return string convertvaluetocolour = "&h" ' split input colors() = split(rgbstring, ",") ' loop in reverse vba (likes gbr not rgb) = ubound(colors()) lbound(colors()) step -1 convertvaluetocolour = convertvaluetocolour & hex(rtrim(ltrim(colors(i)))) next end function ' sub calls above function public sub colora1() range("a1").interior.color = convertvaluetocolour(range("a1").value) end sub
*note have rewritten function removed credit mrexcel
Comments
Post a Comment