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 :
.. 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
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 | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | |||||||||
2 | &H10000 | &H1000 | &H100 | &H10 | &H1 | ||||
3 | B | G | R | ||||||
4 | HexStuff | 65536 | 4096 | 256 | 16 | 1 | .Color = (F$4*F5)+(E$4*E5)+(D$4*D5)+(C$4*C5)+(B$4*B5) | ||
5 | R | 1 | 1 | ||||||
6 | G | 2 | 512 | ||||||
7 | B | 1 | 65536 | ||||||
8 | RGB | 2 | 2 | 2 | 131586 | ||||
Tabelle1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
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