Selecting RGB interior using input box

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Selecting RGB interior using input box

Post by ABabeNChrist »

What I’m trying to achieve is adding an interior color by using RGB to a single cell or mutable cell selection, that when command button is selected an input box will appear asking for Red color # once entered then going to next input box asking for Green color # and then of course input box asking for Blue color #. I have been able to figure out how to get input boxes to appear but not sure how to enter RGB color selection into selected cells.
Here is what I have so far

Code: Select all

Private Sub CommandButton2_Click()
    Dim R As Integer, G As Integer, B As Integer
    Dim FLR As String, FLG As String, FLB As String

    On Error GoTo EH
    FLR = Application.InputBox("Enter Your Desired Red Value", "Adding RGB", Type:=2)
    If FLR = "" Then Exit Sub
    If FLR >= 255 Then Exit Sub

    FLG = Application.InputBox("Enter Your Desired Green Value", "Adding RGB", Type:=2)
    If FLG = "" Then Exit Sub
    If FLG >= 255 Then Exit Sub

    FLB = Application.InputBox("Enter Your Desired Blue Value", "Adding RGB", Type:=2)
    If FLB = "" Then Exit Sub
    If FLB >= 255 Then Exit Sub

    MsgBox "Your Interior RGB Color Selections Are: = " & FLR & ", " & FLG & ", " & FLB
    
    'R = Selection.Interior.Color
    'G = Selection.Interior.Color
    'B = Selection.Interior.Color

    'ActiveCell.Interior.Color = "R, G, B"

EH:     Exit Sub
End Sub

User avatar
HansV
Administrator
Posts: 78544
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Selecting RGB interior using input box

Post by HansV »

255 is allowed, so your tests should be

If FLR > 255 Then Exit Sub

etc.

To set the color:

ActiveCell.Interior.Color = RGB(FLR, FLG, FLB)

If you want to apply it to the entire selection:

Selection.Interior.Color = RGB(FLR, FLG, FLB)
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Selecting RGB interior using input box

Post by ABabeNChrist »

Thank you Hans
I was closer at getting it right than I thought.

User avatar
HansV
Administrator
Posts: 78544
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Selecting RGB interior using input box

Post by HansV »

If you copy the following code into a standard module, you can display a color dialog:

Code: Select all

Private Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pColorStruct As ColorStruct) As Long

Private Const CC_RGBINIT = &H1

Private Type ColorStruct
  lStructSize As Long
  hwndOwner As Long
  hInstance As Long
  rgbResult As Long
  lpCustColors As String
  flags As Long
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As String
End Type

Function ShowColor(Optional lngColor As Long) As Long
  Dim cc As ColorStruct
  Dim CustomColors() As Byte
  Dim i As Integer
  ReDim CustomColors(0 To 16 * 4 - 1) As Byte
  For i = LBound(CustomColors) To UBound(CustomColors)
    CustomColors(i) = 0
  Next i

  'set the structure size
  cc.lStructSize = Len(cc)
  'Set the owner
  
  cc.hwndOwner = Application.Hwnd
  'set the application's instance
  cc.hInstance = 0
  'set the custom colors (converted to Unicode)
  cc.lpCustColors = StrConv(CustomColors, vbUnicode)
  'no extra flags
  cc.flags = CC_RGBINIT
  'set color
  cc.rgbResult = lngColor

  'Show the 'Select Color'-dialog
  If ChooseColor(cc) <> 0 Then
    ShowColor = cc.rgbResult
    'CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
  Else
    ShowColor = -1
  End If
End Function
Your macro could be changed to this:

Code: Select all

Private Sub CommandButton2_Click()
    Dim lngColor As Long
    lngColor = ShowColor
    If lngColor > -1 Then
        Selection.Interior.Color = lngColor
    End If
End Sub
See the attached sample workbbook.
SelectColor.xlsm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Selecting RGB interior using input box

Post by ABabeNChrist »

:grin: :grin: :grin: Very Cool :grin: :grin: :grin: