Excel VBA RGB Color Converter
Public Function ConvertRGBToHEX(iRed As Integer, iGreen As Integer, iBlue As Integer) As String
ConvertRGBToHEX = "#" & Right("0" & Hex(iRed), 2) & Right("0" & Hex(iGreen), 2) & Right("0" & Hex(iBlue), 2)
End Function
Public Function ConvertHexToRGB(sHexColor As String, sRGB As String) As Integer
sHexColor = Replace(sHexColor, "#", "")
Select Case UCase(sRGB)
Case "R"
ConvertHexToRGB = CInt("&h" & Left(sHexColor, 2))
Case "G"
ConvertHexToRGB = CInt("&h" & Mid(sHexColor, 3, 2))
Case "B"
ConvertHexToRGB = CInt("&h" & Right(sHexColor, 2))
End Select
End Function
Public Function ConvertRGBToLong(iRed As Integer, iGreen As Integer, iBlue As Integer) As Long
ConvertRGBToLong = RGB(iRed, iGreen, iBlue)
End Function
Public Function ConvertLongToRGB(lColor As Long, sRGB As String) As Integer
Select Case UCase(sRGB)
Case "R"
ConvertLongToRGB = lColor Mod 256
Case "G"
ConvertLongToRGB = lColor \ 2 ^ 8 Mod 256
Case "B"
ConvertLongToRGB = lColor \ 2 ^ 16 Mod 256
End Select
End Function
Public Function ConvertLongToHex(lColor As Long) As String
Dim sRed As String, sGreen As String, sBlue As String
sRed = Right("00" & Hex(lColor Mod 256), 2)
sGreen = Right("00" & Hex(lColor \ 2 ^ 8 Mod 256), 2)
sBlue = Right("00" & Hex(lColor \ 2 ^ 16 Mod 256), 2)
ConvertLongToHex = "#" & sRed & sGreen & sBlue
End Function
Public Function ConvertHexToLong(sHexColor As String) As Long
Dim sRed As String, sGreen As String, sBlue As String
sHexColor = Replace(sHexColor, "#", "")
sRed = Left(sHexColor, 2)
sGreen = Mid(sHexColor, 3, 2)
sBlue = Right(sHexColor, 2)
ConvertHexToLong = CLng("&h" & sBlue) * 2 ^ 16 + CLng("&h" & sGreen) * 2 ^ 8 + CLng("&h" & sRed)
End Function
Nutty Narwhal