I was trying to incrementally change the background color of a cell to black, and I found that the Range.Interior.Color method returns a Long which is seemingly arbitrary. Looking at the documentation on MSDN, there is nearly nothing about what this number represents. Is there a way to return the RGB value from this long. I effectively need the opposite of the RGB(red, green, blue) function.
相关问题
- Excel sunburst chart: Some labels missing
- DBGrid - How to set an individual background color
- Error handling only works once
- Error handling only works once
- Excel formula in VBA code
相关文章
- Get column data by Column name and sheet name
- Emacs/xterm color annoyance on Linux
- programmatically excel cells to be auto fit width
- Unregister a XLL in Excel (VBA)
- Unregister a XLL in Excel (VBA)
- matplotlib bwr-colormap, always centered on zero
- How to prevent excel from truncating numbers in a
- numeric up down control in vba
Short Answer:
There is no built in functionality for this. You must write your own function.
Long Answer:
The long that is returned from the Interior.Color property is a decimal conversion of the typical hexidecimal numbers that we are used to seeing for colors in html e.g. "66FF66". Additionally the constant xlNone (-4142) can be passed to set cell to have no color in the background, however such cells are marked white
RGB(255, 255, 255)
from theGet
property. Knowing this, we can write a function that returns one or all of the appropriate RGB values.Luckily, a kind Mr. Allan Wyatt has done just that here!
Determining the RGB Value of a Color
good to see that Mr Wyatt uses the fast method of color to RGB
which is many times faster than those using hex str with left mid right that some recommend
Should note, for the hex values, if you're exporting out to HTML you're going to get quirks too.
Ideally you'd create the hex string from the individual colours, rather than returning a hex from the ColorVal number.
The reason being you can get some invalid hex numbers if the cell is a 'pure' colour like green/blue
RED - RGB(255,0,0) returns 'FF' - it should return 'FF0000'
BLUE - RGB(0,0,255) returns 'FF00000' - it should return '0000FF'
enter image description here
If you used these to create HTML/CSS colour output, you'd get RED for any blue cells.
I modified the script to assemble each two character hex 'chunk' based on the RGB values, with a UDF that just pads with a leading 0 where output of one character is returned ( hopefully if you're reading this, you can make something similar )
--Edit : forgot to include the code for the UDF...
Mark Balhoff´s VBA script works fine. All credits go to him.
In case you´d like to get the color codes/indexes of conditionally formatted cells as well, the code may be amended like this:
That "arbitrary" number is a mathematical combination of the RGB values (B*256^2 + G*256 + R) and a conversion of the hex color value to a decimal number (base 16 to base 10), depending on which way you want to look at it. Just different bases. Below is the method I use in the XLAM addin file I wrote for Excel. This method has come in handy many times. I have included the documentation in my addin file.
The other answer did not work for me. I found that:
and it worked properly.