Excel VBA

css navigation by Css3Menu.com

Convert HEX to RGB

Looking through my pages, you have probably figured out that I do a lot with color. A client a few years ago had specific colors that need to be used in reports. I wrote a handy-dandy RGB to HEX (and vice versa) dialog to do that from my toolbars.

Convert HEX
Private Sub HexVal()
    If Me.mltMultiForm.Value = 1 Then
        lblHeading.Caption = _
            "Insert 3 HEX values in the spaces provided and they will be converted to RGB. Remember, HEX values are 2 characters; between ии & FF."
    Else
        lblHeading.Caption = "Insert 3 RGB values in the spaces provided and they will be converted to HEX"
    End If
    Me.lblRGBvalues.Caption = Application.Hex2Dec(txtHex1.Value) & "," & _
            Application.Hex2Dec(txtHEX2.Value) & "," & Application.Hex2Dec(txtHEX3.Value)
    Me.lblHEXcolor.BackColor = RGB(Application.Hex2Dec(txtHex1.Value), Application.Hex2Dec(txtHEX2.Value), Application.Hex2Dec(txtHEX3.Value))
    Me.lblHEXcolor.Caption = Me.txtHex1.Value & Chr(32) & "-" & Chr(32) & Me.txtHEX2.Value _
            & Chr(32) & "-" & Chr(32) & Me.txtHEX3.Value

End Sub

Private Sub RGBvalS()
    On Error Resume Next
        If Me.mltMultiForm.Value = 1 Then
        lblHeading.Caption = _
            "Insert 3 HEX values in the spaces provided and they will be converted to RGB. Remember, HEX values are 2 characters; between ии & FF."
    Else
        lblHeading.Caption = "Insert 3 RGB values in the spaces provided and they will be converted to HEX"
    End If
    Me.lblHexValues.Caption = Application.Dec2Hex(txtR.Value) & _
            Application.Dec2Hex(txtG.Value) & Application.Dec2Hex(txtB.Value)
    Me.lblColorRep.BackColor = RGB(Me.txtR.Value, Me.txtG.Value, Me.txtB.Value)
    Me.lblColorRep.Caption = Me.txtR.Value & Chr(32) & "-" & Chr(32) & Me.txtG.Value _
            & Chr(32) & "-" & Chr(32) & Me.txtB.Value

End Sub

Not only does it provide me with the conversion, it also shows me a close representation of the color on the fly in the dialog.

Convert RGB

© 2013-2024

Updated:  06/21/2024 07:42
This page added:  27 March 2013