color palette

lpvdsteen

Board Regular
Joined
Jan 21, 2003
Messages
161
Office Version
  1. 2016
Platform
  1. Windows
Hi
Is there a way to, from VBA, have the colorpalette pop up and allow the user to select a color which VBA can the assign to a designated cel, listbox or whatever. With this palette in the system, it seems so pointless to redisign it on a userform.
Luke
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Yes it is...here is One way..amend as required or post back
for Help.

Note it is NOT the Excel Dialog


<pre><FONT COLOR="#00007F">Option</FONT> <FONT COLOR="#00007F">Explicit</FONT>
<FONT COLOR="#007F00">'// Ivan F Moala</FONT>
<FONT COLOR="#007F00">'// http://www.XcelFiles.com</FONT>

<FONT COLOR="#00007F">Private</FONT> Type CHOOSECOLOUR
lStructSize <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>
hwndOwner <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>
hInstance <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>
rgbResult <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>
lpCustColors <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">String</FONT>
flags <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>
lCustData <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>
lpfnHook <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>
lpTemplateName <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">String</FONT>
End Type

<FONT COLOR="#00007F">Declare</FONT> <FONT COLOR="#00007F">Function</FONT> ChooseColorA _
<FONT COLOR="#00007F">Lib</FONT> "Comdlg32" ( _
lpChooseColor <FONT COLOR="#00007F">As</FONT> CHOOSECOLOUR) _
<FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>

<FONT COLOR="#00007F">Declare</FONT> <FONT COLOR="#00007F">Function</FONT> FindWindowA _
<FONT COLOR="#00007F">Lib</FONT> "User32" ( _
<FONT COLOR="#00007F">ByVal</FONT> lpClassName <FONT COLOR="#00007F">As</FONT> Any, _
<FONT COLOR="#00007F">ByVal</FONT> lpWindowName <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">String</FONT>) _
<FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>


<FONT COLOR="#00007F">Function</FONT> SelectColour(Code_RGB) <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Boolean</FONT>
<FONT COLOR="#00007F">Dim</FONT> CColor <FONT COLOR="#00007F">As</FONT> CHOOSECOLOUR
<FONT COLOR="#00007F">Dim</FONT> CustColors <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">String</FONT> * 16

<FONT COLOR="#00007F">With</FONT> CColor
.lStructSize = 36
.hwndOwner = FindWindowA(0&, Application.Caption)
.lpCustColors = CustColors
.flags = 2
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">With</FONT>

<FONT COLOR="#00007F">If</FONT> ChooseColorA(CColor) = 0 <FONT COLOR="#00007F">Then</FONT> <FONT COLOR="#00007F">Exit</FONT> <FONT COLOR="#00007F">Function</FONT>
Code_RGB = CColor.rgbResult
SelectColour = <FONT COLOR="#00007F">True</FONT>

<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Function</FONT>

<FONT COLOR="#00007F">Sub</FONT> Tester()
<FONT COLOR="#00007F">Dim</FONT> Code_RGB <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>

<FONT COLOR="#00007F">If</FONT> Not SelectColour(Code_RGB) <FONT COLOR="#00007F">Then</FONT> <FONT COLOR="#00007F">Exit</FONT> <FONT COLOR="#00007F">Sub</FONT>

MsgBox "The RGB code you chose:=" & Code_RGB & vbCr & _
"Red:=" & RGBRed(Code_RGB) & vbCr & _
"Green:=" & RGBGreen(Code_RGB) & vbCr & _
"Blue:=" & RGBBlue(Code_RGB)

<FONT COLOR="#007F00">'// Activecell colour</FONT>
ActiveCell.Interior.Color = Code_RGB

<FONT COLOR="#007F00">'// OR Selection</FONT>
<FONT COLOR="#007F00">'Selection.Interior.Color = Code_RGB</FONT>

<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Sub</FONT>

<FONT COLOR="#00007F">Public</FONT> <FONT COLOR="#00007F">Function</FONT> RGBRed(RGBCol <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>) <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Integer</FONT>
<FONT COLOR="#007F00">'// Return the Red component from an RGB Coloor</FONT>
RGBRed = RGBCol And &HFF
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Function</FONT>

<FONT COLOR="#00007F">Public</FONT> <FONT COLOR="#00007F">Function</FONT> RGBGreen(RGBCol <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>) <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Integer</FONT>
<FONT COLOR="#007F00">'// Return the Green component from an RGB Colour</FONT>
RGBGreen = ((RGBCol And &H100FF00) / &H100)
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Function</FONT>

<FONT COLOR="#00007F">Public</FONT> <FONT COLOR="#00007F">Function</FONT> RGBBlue(RGBCol <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>) <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Integer</FONT>
<FONT COLOR="#007F00">'// Return the Blue component from an RGB Colour</FONT>
RGBBlue = (RGBCol And &HFF0000) / &H10000
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Function</FONT>
</pre>
 
Upvote 0
Haven'tbeen on the net lately, so a bit late in my reaction. (work.work.work)

This works great, however

I was looking for the excel-systems palette. The problem with the windows palette is that is is too advanced for excel (excel can only work with a limited amount of colours and so when you select a particular colour it may turn out completely different.) Can i adress this excel-palette in the same way and how, I presume (hope) it is just another library?

Luke
 
Upvote 0
Ok, that does the trick. Tought me a deal again as well, fun. I was hesitant to use a form to reinvent the wheel, but the amount of data used it minimal, and the function extreem. Thanks

Luke
 
Upvote 0

Forum statistics

Threads
1,215,756
Messages
6,126,692
Members
449,330
Latest member
ThatGuyCap

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