Hello,
the following macro function (see end of this post) does what you want. Just put it into Tools → Macros → “Edit Macros” and you can call it from inside any cell via
=IFCOLOR("A2","ff0000", A2/A3,-1)
- The 1st argument is the cell to check quoted as a string
- The 2nd argument is a hexadecimal color value quoted as a string To view or set the hex value of a cell open the color picker ref1. ref2..
- the 3rd argument is the result to set when the background color matches
- the 4th argument is the result to set when the background color deos not match
Example Screenshot:
Here the macro function you have to import:
Function IFCOLOR(cellName As String, hexColor As String, ifValue as variant, elseValue as variant)
REM remove trailing spaces
cellName = Trim(cellName)
hexColor = UCase(Trim(hexColor))
Dim decColor As Long
REM check length
if (Len(hexColor) <> 6) then
SUMIFCOLOR = "ERR: invalid hex code, length <> 6"
Exit Function
endif
REM check chars
For i = 1 To Len(hexColor)
c = Mid(hexColor, i, 1)
Select Case c
Case "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
Case Else
SUMIFCOLOR = "ERR: invalid hex code, invalid hex char"
Exit Function
End Select
Next
REM convert to long
decColor = CLng("&H" & hexColor) REM convert to long
REM sum result
sum = 0.0
Set oSheet = ThisComponent.CurrentController.ActiveSheet
Set oCell = oSheet.getCellRangeByName(cellName)
if(oCell.CellBackColor = decColor) then
IFCOLOR = ifValue
else
IFCOLOR = elseValue
endif
End Function
Hope that helps.
To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!