I found this code in the web, it seemed to be useful, so I inserted this function in the calc spreadsheet.
Function SumByColor(oCellRef As Range, oSumRange As Range) As Double
Dim oSheet As Object
Dim oCell As Object
Dim targetColor As Long
Dim total As Double
Dim r As Long, c As Long
Dim uR As Long, uC As Long
' Get target background color from the reference cell
targetColor = oCellRef.CellBackColor
total = 0.0
' Parse the target range dimensions
uR = oSumRange.Rows.Count - 1
uC = oSumRange.Columns.Count - 1
' Loop through all cells in the range
For r = 0 To uR
For c = 0 To uC
set oCell = oSumRange.getCellByPosition(c, r)
If oCell.CellBackColor = targetColor Then
If IsNumeric(oCell.Value) Then
total = total + oCell.Value
End If
End If
Next c
Next r
SumByColor = total
End Function
Unfortunately, when I try to call this function writing in a cell
=SUMBYCOLOR(E340; D2:D341)
the code fails immediately (apparently on the function definition line) with tjhe error “Object varioable not set”. I am a real beginner with libre office, and I really don’t know what is wrong with the code. I found a lot of similar entries in this forum, but none of the solutions proposed works.
Thank you for your understanding
Libre office version 26.2.5.2 (x86,64) on windows 11.

because range = D3:D18 (from default index = 1).



