Interior.Color RGB Values

DocAElstein

Banned user
Joined
May 24, 2014
Messages
1,336
Interior.Color RGB Values

Hello
… I am almost there on this one and need a last bit of help on my third code……

. I have written 3 codes to return the Interior.Color RGB Values of a cell. They all work, that is to say they give me the R G B integer values for the Interior Color of any cell (In my examples here it is Cell (1, 1) of the Active Sheet).
. The first code I wrote completely myself and is stupid because it takes ages!!!( The Program simply goes through every single color format number given by property RGB(R,G,B) and compares it with that single color format number given by the .Color Property applied to cell (1,1) until a match is found.)

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' Code 1</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> FindInteriorColorRGBColourIndexLongWay1()<br><SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> TheEnd <SPAN style="color:#007F00">'Stop program (and reset Status Bar) if anything goes wrong</SPAN><br><SPAN style="color:#007F00">' This Program simply goes through every color number given by RGB(R.G,B) and compares it with that given by the .Color Property until a match is found.</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> R <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, G <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, B <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">' R G B are from 0 to 255 so a Byte size variable is sufficient</SPAN><br><SPAN style="color:#00007F">For</SPAN> R = 0 <SPAN style="color:#00007F">To</SPAN> 255 <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">' For every R value go.....</SPAN><br>  <SPAN style="color:#00007F">For</SPAN> G = 0 <SPAN style="color:#00007F">To</SPAN> 255 <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">' ...for every G value go....</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> B = 1 <SPAN style="color:#00007F">To</SPAN> 255 <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">'....through every B value....</SPAN><br>      Application.StatusBar = R & "  " & G & "  " & B <SPAN style="color:#007F00">' give current R G B values in Left Under Window corner</SPAN><br>      <SPAN style="color:#00007F">If</SPAN> ActiveSheet.Cells(1, 1).Interior.Color = RGB(R, G, B) <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">'....until a match is found</SPAN><br>        MsgBox ("R" & vbTab & "G" & vbTab & "B is " & R & vbTab & G & vbTab & B)<br>        <SPAN style="color:#00007F">GoTo</SPAN> TheEnd<br>        <SPAN style="color:#00007F">Else</SPAN><br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> B = 255 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#007F00">' Extra line needed to prevent Byte being given value > 255</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> B<br>  <SPAN style="color:#00007F">If</SPAN> G = 255 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN><br>  <SPAN style="color:#00007F">Next</SPAN> G<br><SPAN style="color:#00007F">If</SPAN> R = 255 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN><br><SPAN style="color:#00007F">Next</SPAN> R<br>TheEnd:<br>Application.StatusBar = <SPAN style="color:#00007F">False</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'FindInteriorColorRGBColourIndexLongWay1()</SPAN><br><SPAN style="color:#007F00">'______________________________________________________________________________________-</SPAN><br><SPAN style="color:#007F00">'_______________________________________________________________________________</SPAN><br><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN></FONT>

. The second and third codes I found by a bit of “Googeling”, trial and error, by racking my brain, and playing around in Excel with Tables such as :


Book1
ABCDEFG
1
2&H10000&H1000&H100&H10&H1
3BGR
4HexStuff655364096256161.Color = (F$4*F5)+(E$4*E5)+(D$4*D5)+(C$4*C5)+(B$4*B5)
5R11
6G2512
7B165536
8RGB222131586
Tabelle1
Cell Formulas
RangeFormula
G5=(F$4*F5)+(E$4*E5)+(D$4*D5)+(C$4*C5)+(B$4*B5)
G6=(F$4*F6)+(E$4*E6)+(D$4*D6)+(C$4*C6)+(B$4*B6)
G7=(F$4*F7)+(E$4*E7)+(D$4*D7)+(C$4*C7)+(B$4*B7)
G8=(F$4*F8)+(E$4*E8)+(D$4*D8)+(C$4*C8)+(B$4*B8)


.. I managed to understand the second code and have written appropriate comments:-

<font face=Calibri><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN><br><SPAN style="color:#007F00">' Code 2</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> FindInteriorColorRGBColourIndexUsingHexStuff2()<br><SPAN style="color:#00007F">Dim</SPAN> R <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, G <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, B <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, RGBColour <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    RGBColour = ActiveSheet.Cells(1, 1).Interior.Color<br>    R = RGBColour Mod 256 <SPAN style="color:#007F00">' Mod gives what is left over from RGBColour after dividing by 256 : All .color numbers are = (multiples of 256 for green and Blue: (256,512....(255 x 256)) for green   ;  (65536,131072.....(255 x 65536) for Blue))  +  (0 to 255 depending on the Rot value)). So here the Mod blocks out Green and Blue multiples, and leaves just the 0 - 255 step 1 integer which represents Red</SPAN><br>    G = (RGBColour Mod 256 ^ 2) \ 256 <SPAN style="color:#007F00">' Mod gives what is over after dividing by 256^2=65536. This blocks out numbers which include multiples of 65536 which are Blue numbers,(As Blue numbers are multiples of 65536). \ is divide, but only return integer part. This gives numbers which are the exact multiples of 256(which are Green numbers). This effectively blocks out the Red part of the number (0-255).</SPAN><br>    B = RGBColour \ (256 ^ 2) <SPAN style="color:#007F00">'\ is divide, but only return integer part. This gives numbers which are the exact multiples of 65536(which are Blue numbers). This effectively blocks out the Red part of the number (0-255 step 1), and the green part (256-65280 step 256)</SPAN><br> MsgBox ("R" & vbTab & "G" & vbTab & "B" & vbTab & "RGBColour is " & R & vbTab & G & vbTab & B & vbTab & RGBColour)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'FindInteriorColorRGBColourIndexUsingHexStuff2()</SPAN><br><SPAN style="color:#007F00">'_______________________________________________________________________________________-</SPAN><br><SPAN style="color:#007F00">'_______________________________________________________________________________</SPAN><br><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN></FONT>

… I just need help to understand my third code below……


<font face=Calibri><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN><br><SPAN style="color:#007F00">' Code 3</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> FindInteriorColorRGBColourIndexUsingHexStuff3()<br>    <SPAN style="color:#00007F">Dim</SPAN> R <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, G <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, B <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, RGBColour <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    RGBColour = ActiveSheet.Cells(1, 1).Interior.Color<br>    R = RGBColour And vbRed <SPAN style="color:#007F00">'vbRed = 255  .....???  ... wot is happening with AND here???</SPAN><br>    G = (RGBColour And vbGreen) <SPAN style="color:#007F00">' ....wot is happening with AND here??? ???</SPAN><br>    G = (RGBColour And vbGreen) \ &H100 <SPAN style="color:#007F00">'vbGreen = 255 x 256 = 65280 ; &H100 = (256 x 1) + (16 x 0) + (1 x 0) = 256</SPAN><br>    B = (RGBColour And vbBlue) <SPAN style="color:#007F00">' ....wot is happening with AND here??? ???</SPAN><br>    B = (RGBColour And vbBlue) \ &H10000 <SPAN style="color:#007F00">'vbBlue = (256^2) x 255 = 16711680 ; &H10000 = (65536 x 1) + (4096 x 1) + (256 x 0) + (16 x 0) + (1 x 0) = 65536</SPAN><br>   MsgBox ("R" & vbTab & "G" & vbTab & "B" & vbTab & "RGBColour is " & R & vbTab & G & vbTab & B & vbTab & RGBColour)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'FindInteriorColorRGBColourIndexUsingHexStuff3()</SPAN><br><SPAN style="color:#007F00">'_______________________________________________________________________________</SPAN><br><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN></FONT>

.. I am a beginner and am keen to understand exactly what is going on.. I am stuck on understanding this third code. Probably I do not fully understand what the And is doing in this code . I have used And lots in conditional stuff, but I just do not see what it is doing in the third Code.
Can anyone explain this to me and / or suggest any alternative codes?

Thanks
Alan
Bavaria

P.s. Full Excel files in XL2003 and XL 2007 with macros in module “FindCellInteriorRGBValues” are here:
FileSnack | Easy file sharing
FileSnack | Easy file sharing
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Here's a workbook function to get hex:

Code:
Function HexCode(Cell As Range) As String    
HexCode = Right("000000" & Hex(Cell.Interior.Color), 6)
End Function

This returns the hex code in RGB order

Code:
Function getRGB1(rcell) As String
    Dim sColor As String


    sColor = Right("000000" & Hex(rcell.Interior.Color), 6)
    getRGB1 = Right(sColor, 2) & Mid(sColor, 3, 2) & Left(sColor, 2)
End Function

This will return the RGB value in the cell using a worksheet function:

Code:
Function getRGB2(rcell) As String
    Dim C As Long
    Dim R As Long
    Dim G As Long
    Dim B As Long


    C = rcell.Interior.Color
    R = C Mod 256
    G = C \ 256 Mod 256
    B = C \ 65536 Mod 256
    getRGB2 = "R=" & R & ", G=" & G & ", B=" & B
End Function
 
Upvote 0
Hi NeonRedSharpie
. Thanks for the extra Functions. I got my teeth into them, got them to work, and understand them. I re wrote them with comments to help me understand / remember what is going on.

<font face=Calibri><br><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><SPAN style="color:#007F00">'Here's a workbook function to get hex(In B G R order which is the default order):</SPAN><br><SPAN style="color:#00007F">Function</SPAN> HexCode(Cell <SPAN style="color:#00007F">As</SPAN> Range) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> NumberInHex <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Let</SPAN> NumberInHex = Hex(Cell.Interior.Color) <SPAN style="color:#007F00">'Hex gives Interior.color number in hexadecimal</SPAN><br>    HexCode = Right("000000" & NumberInHex, 6) <SPAN style="color:#007F00">' This part ensures that 6 digits are given (with a extra zero at the start if the B number is less than 15 (or f)</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><SPAN style="color:#007F00">'This returns the hex code in RGB order</SPAN><br><SPAN style="color:#00007F">Function</SPAN> getRGB1(rcell <SPAN style="color:#00007F">As</SPAN> Range) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> NumberInHex <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Let</SPAN> NumberInHex = Hex(rcell.Interior.Color)  <SPAN style="color:#007F00">'Hex gives Interior.color number in hexadecimal</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> sColor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Let</SPAN> sColor = Right("000000" & NumberInHex, 6) <SPAN style="color:#007F00">' This part ensures that 6 digits are given (with a extra zero at the start if the B number is less than 15 (or f)</SPAN><br>    getRGB1 = Right(sColor, 2) & Mid(sColor, 3, 2) & Left(sColor, 2) <SPAN style="color:#007F00">'Chops up the 6 digits into 3 lots of 2 in R G B order</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><SPAN style="color:#007F00">'This will return the RGB value in the cell using a worksheet function:</SPAN><br><SPAN style="color:#00007F">Function</SPAN> getRGB2(rcell <SPAN style="color:#00007F">As</SPAN> Range) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> C <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> R <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> G <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> B <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    C = rcell.Interior.Color<br>    R = C Mod 256 <SPAN style="color:#007F00">'same as to my code 2: Mod gives what is left over from RGBColour after dividing by 256 : All .color numbers are = (multiples of 256 for green and Blue: (256,512....(255 x 256)) for green   ;  (65536,131072.....(255 x 65536) for Blue))  +  (0 to 255 depending on the Rot value)). So here the Mod blocks out Green and Blue multiples, and leaves just the 0 - 255 step 1 integer which represents Red</SPAN><br>    G = C \ 256 Mod 256 <SPAN style="color:#007F00">' Similar to my code 2 : \ 256 blocks out the Red Bit and Mod 256 blocks out the Blue Bit</SPAN><br>    B = C \ 65536 <SPAN style="color:#007F00">'Mod 256 ' Similar to my code 2 : \ 65536 gives the number of B (Mod is not necerssary)</SPAN><br>    getRGB2 = "R=" & R & ", G=" & G & ", B=" & B<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><br><br><SPAN style="color:#00007F">Sub</SPAN> TestNeonRedSharpie()<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> HexStuff1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><SPAN style="color:#00007F">Let</SPAN> HexStuff1 = HexCode(Range("A1"))<br>Debug.Print HexStuff1<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> HexStuff2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><SPAN style="color:#00007F">Let</SPAN> HexStuff2 = getRGB1(Cells(1, 1))<br>Debug.Print HexStuff2<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> HexStuff3 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><SPAN style="color:#00007F">Let</SPAN> HexStuff3 = getRGB2(Cells(1, 1))<br>Debug.Print HexStuff3<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'TestNeonRedSharpie()</SPAN></FONT>


It is all useful stuff for a beginner (Note your last function is similar to my code 2, and a small correction… I do not think you need the Mod 256 bit)

Alan

P.s.
. I am still interested in an explanation of what is happening in my code 3, especially the And bit, if anyone has any ideas.
 
Upvote 0
Hi,
. Just asking again for any ideas on my main problem:- That is I still do not understand my last code (Code 3), that is to say it is not clear to me what And is doing in this case…
…Any fresh ideas?? I just do not understand what the And does when applied to 2 numbers. As I mentioned, I have no problem using and understanding And in typical conditional statements.. That is to say typical lines like
. If a=1 And b=2 Then …. Etc.

Thanks
 
Upvote 0
The VBA And operator does a bitwise Boolean And on two 32-bit integers (i.e., Longs). The value of vbBlue is hex FF0000, so if you and that with some random color, you get just the blue component.
 
Upvote 0
The VBA And operator does a bitwise Boolean And on two 32-bit integers (i.e., Longs). The value of vbBlue is hex FF0000, so if you and that with some random color, you get just the blue component.

Hi shg,
. Great. Thanks. I get it now.
. I’m very grateful.
Alan.

P.s. Revised Code 3 below with comments for anyone stumbling on this Thread in the future. ( Note once you understand wot is going on you can see that a normal divide / is sufficient for this code, and not the integer divide \ which is necessary for Code 2)

<font face=Calibri><SPAN style="color:#007F00">'_______________________________________________________________________________________-</SPAN><br><SPAN style="color:#007F00">'_______________________________________________________________________________</SPAN><br><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN><br><SPAN style="color:#007F00">' Code 3</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> FindInteriorColorRGBColourIndexUsingHexStuff3()<br>****<SPAN style="color:#00007F">Dim</SPAN> R <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, G <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, B <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, RGBColour <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>****RGBColour = ActiveSheet.Cells(1, 1).Interior.Color<br>****R = RGBColour And vbRed <SPAN style="color:#007F00">'vbRed = 255 or in hex 0000FF.** And does a Boolean And for every Bit in the hexadecimal format.**So for vbRed (0000FF) And RGBColor the And function returns the Red Part of the number as this is the only part that is in both of the numbers! This will be a number from 0 to 255</SPAN><br>****G = (RGBColour And vbGreen) / &H100 <SPAN style="color:#007F00">'vbGreen = 255 x 256 = 65280 or 00FF00 in Hex ; &H100 = (256 x 1) + (16 x 0) + (1 x 0) = 256.**And does a Boolean And for every Bit in the hexadecimal format.**So for vbgreen (00FF00) And RGBColor the And function returns the green Part of the number as this is the only part that is in both of the numbers! This will be a number from 0 to 65280. Dividing this number by 256 gives the integer G value</SPAN><br>****B = (RGBColour And vbBlue) / &H10000 <SPAN style="color:#007F00">'vbBlue = (256^2) x 255 = 16711680 or FF0000 in Hex ; &H10000 = (65536 x 1) + (4096 x 1) + (256 x 0) + (16 x 0) + (1 x 0) = 65536.**And does a Boolean And for every Bit in the hexadecimal format.**So for vbBlue (FF0000) And RGBColor the And function returns the Blue Part of the number as this is the only part that is in both of the numbers! This will be a number from 0 to 16712194. Dividing this number by 65536 gives the integer B value</SPAN><br>** MsgBox ("R" & vbTab & "G" & vbTab & "B" & vbTab & "RGBColour is " & R & vbTab & G & vbTab & B & vbTab & RGBColour)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'FindInteriorColorRGBColourIndexUsingHexStuff3()</SPAN><br><SPAN style="color:#007F00">'_______________________________________________________________________________</SPAN><br><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN></FONT>
 
Last edited:
Upvote 0
Code again without the **** Bug which cak´me in because i edited the thread!!!



<font face=Calibri><SPAN style="color:#007F00">'_______________________________________________________________________________________-</SPAN><br><SPAN style="color:#007F00">'_______________________________________________________________________________</SPAN><br><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN><br><SPAN style="color:#007F00">' Code 3</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> FindInteriorColorRGBColourIndexUsingHexStuff3()<br>    <SPAN style="color:#00007F">Dim</SPAN> R <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, G <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, B <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, RGBColour <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    RGBColour = ActiveSheet.Cells(1, 1).Interior.Color<br>    R = RGBColour And vbRed <SPAN style="color:#007F00">'vbRed = 255 or in hex 0000FF.   And does a Boolean And for every Bit in the hexadecimal format.  So for vbRed (0000FF) And RGBColor the And function returns the Red Part of the number as this is the only part that is in both of the numbers! This will be a number from 0 to 255</SPAN><br>    G = (RGBColour And vbGreen) / &H100 <SPAN style="color:#007F00">'vbGreen = 255 x 256 = 65280 or 00FF00 in Hex ; &H100 = (256 x 1) + (16 x 0) + (1 x 0) = 256.  And does a Boolean And for every Bit in the hexadecimal format.  So for vbgreen (00FF00) And RGBColor the And function returns the green Part of the number as this is the only part that is in both of the numbers! This will be a number from 0 to 65280. Dividing this number by 256 gives the integer G value</SPAN><br>    B = (RGBColour And vbBlue) / &H10000 <SPAN style="color:#007F00">'vbBlue = (256^2) x 255 = 16711680 or FF0000 in Hex ; &H10000 = (65536 x 1) + (4096 x 1) + (256 x 0) + (16 x 0) + (1 x 0) = 65536.  And does a Boolean And for every Bit in the hexadecimal format.  So for vbBlue (FF0000) And RGBColor the And function returns the Blue Part of the number as this is the only part that is in both of the numbers! This will be a number from 0 to 16712194. Dividing this number by 65536 gives the integer B value</SPAN><br>   MsgBox ("R" & vbTab & "G" & vbTab & "B" & vbTab & "RGBColour is " & R & vbTab & G & vbTab & B & vbTab & RGBColour)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'FindInteriorColorRGBColourIndexUsingHexStuff3()</SPAN><br><SPAN style="color:#007F00">'_______________________________________________________________________________</SPAN><br><SPAN style="color:#007F00">'__________________________________________________________________________________</SPAN><br><br><SPAN style="color:#007F00">'______________________________________________________________</SPAN></FONT>
 
Upvote 0
I'm more likely to read your code in future posts if it's in code tags, which, curiously, is suggested in your signature.

Four-point font in Technicolor attracts me not.
 
Upvote 0
I'm more likely to read your code in future posts if it's in code tags, which, curiously, is suggested in your signature.

Four-point font in Technicolor attracts me not.



Hi shg,
. Thanks again for that feedback- I appreciate it. I was already thinking along the lines you said - that my codes were a bit over the top for a profi to read, and was trying to simplify it… at least to remove the long rambling comments…I had already asked about that http://www.mrexcel.com/forum/about-board/795476-comments-code.html but no reply to date.
. I first made the effort to get into the HTML Code stuff, rather than simple Code tags after seeing and hearing about it from other moderators! (http://www.mrexcel.com/forum/questi...rch-workbooks-oder-windows-oop-methode-2.html http://www.mrexcel.com/forum/about-board/780464-re-text-format-forums-o00o-`-_-`-o00o.html ). But they also suggest simple Code Tags as well as the HTML stuff!!
. Lots of comments I thought was useful for other normal users and beginners like me viewing the thread at some time later, but again a pain for some Profis. Can’t please everyone, all the time!. But I’ll try to keep simple short no comment codes in Threads when you are on line.
Thanks again
Alan

P.s last couple of simple Codes (Codes 2 and 3) then for getting the R G B values for the interior color of a cell

' Code 3
Code:
Sub FindInteriorColorRGBColourIndexUsingHexStuff3NoComments()
    Dim R As Byte, G As Byte, B As Byte, RGBColour As Long
    RGBColour = ActiveSheet.Cells(1, 1).Interior.Color
    R = RGBColour And vbRed
    G = (RGBColour And vbGreen) / &H100
    B = (RGBColour And vbBlue) / &H10000
   MsgBox ("R" & vbTab & "G" & vbTab & "B" & vbTab & "RGBColour is " & R & vbTab & G & vbTab & B & vbTab & RGBColour)
End Sub


' Code 2
Code:
Sub FindInteriorColorRGBColourIndexUsingHexStuff2NoComments()
Dim R As Byte, G As Byte, B As Byte, RGBColour As Long
    RGBColour = ActiveSheet.Cells(1, 1).Interior.Color
    R = RGBColour Mod 256
    G = (RGBColour Mod 256 ^ 2) \ 256
    B = RGBColour \ (256 ^ 2)
 MsgBox ("R" & vbTab & "G" & vbTab & "B" & vbTab & "RGBColour is " & R & vbTab & G & vbTab & B & vbTab & RGBColour)
End Sub

All codes again (with and without comments!) here:

FileSnack | Easy file sharing

FileSnack | Easy file sharing

Have a Great day
 
Upvote 0

Forum statistics

Threads
1,224,872
Messages
6,181,501
Members
453,047
Latest member
charlie_odd

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