Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello @Michael Fontaine, and @Hopeful-LO-user

In addition to the macro given by @Lupp, which returns the RGB values of the Cell Background Color as an Array, the following macro returns the color’s RGB values as a single Long Integer, and the next following macro returns the color’s HEX values as a String.

Just call in your Calc cell: = getCellBackColorRGB(“A1”) or =getCellBackColorHEX( “A1” )

Function getCellBackColorRGB( strCellAddress$ ) As Long
REM <strCellAddress>:   pass a Cell address in the current Sheet, such as "A1" or "$A$1".
REM Returns a Long Integer representing the RGB values of the Background Color of the specified Cell.
    Dim oSheet As Object : oSheet = ThisComponent.CurrentController.ActiveSheet
    Dim oCell  As Object : oCell  = oSheet.getCellRangeByName( strCellAddress )
    getCellBackColorRGB  = oCell.CellBackColor
End Function

Function getCellBackColorHEX( strCellAddress$ ) As String
REM <strCellAddress>:   pass a Cell address in the current Sheet, such as "A1" or "$A$1".
REM Returns a String representing the HEX values of the Background Color of the specified Cell.
    Dim oSheet As Object : oSheet = ThisComponent.CurrentController.ActiveSheet
    Dim oCell  As Object : oCell  = oSheet.getCellRangeByName( strCellAddress )
    getCellBackColorHEX  = RGBtoHEX( oCell.CellBackColor )
End Function

Function RGBToHex( lRGB As Long ) As String
    RGBToHex = ByteToHex( Red( lRGB ) ) & ByteToHex( Green( lRGB ) ) & ByteToHex( Blue( lRGB ) )
End Function

Function ByteToHex( iByte As Integer ) As String
    Dim strHex As String    : strHex = Hex( iByte )
    If Len( strHex ) = 1 Then strHex = "0" & strHex
    ByteToHex = strHex
End Function

HTH, lib