Hex

Public Function Hex(ByVal Number As Long) As String

Returns the uppercase hex value (without any prefixes) for the specified number.

Public Function WindowsColorToHTML(ByVal pColor As Long) As String
   Dim lRed As Long = pColor And 255
   pColor = pColor \ 256 'Shift 8 bits
   
   Dim lGreen As Long = pColor And 255
   pColor = pColor \ 256 'Shift 8 bits
   
   Dim lBlue As Long = pColor And 255
   
   Return $"#{ColorComponentToHexPart(lRed)}{ColorComponentToHexPart(lGreen)}{ColorComponentToHexPart(lBlue)}"
End Function

Private Function ColorComponentToHexPart(pColorComponent As Long) As String
       'Take hex, prepend a leading zero (so then always at least 2 chars) and return the last 2 digits.
   Return Right("0" & Hex(pColorComponent), 2)
End Function

See also: