Calc: Find replace background color in cell range

How do I replace the background color in a specified range of cells when the same color is used in other cells? As an example: I have a sheet that contains a bunch of cells in RGB(0,106,199) but I only want to replace a certain subset of them with RGB(255,0,0) (i.e., turn the blue background to red).

Note: This is a follow-up question to a question about replacing the background color of all cells in a spreadsheet (original question is here).

Hello again @RachelAB,

Please try if the following macro works for you:

Sub cellRange_ReplaceBackgroundColor( strRange as String, lFindColor as Long, lReplaceColor as Long )
REM Replace only a certain color within a range.
REM Example call: cellRange_ReplaceBackgroundColor( "A14:D15", RGB(255,0,0), RGB(0,255,0) )
	Dim oSheet As Object  : oSheet = ThisComponent.CurrentController.ActiveSheet
	Dim oRange As Object  : oRange = oSheet.getCellRangebyName( strRange )
	Dim oCell As Object
	Dim i As Integer, j As Integer
	For i = 0 To oRange.Rows.getCount() - 1				REM Traverse all cells in the specified range:
		For j = 0 To oRange.Columns.getCount() - 1
			oCell = oRange.getCellByPosition( j, i )
			If oCell.CellBackColor = lFindColor Then oCell.CellBackColor = lReplaceColor
		Next j
	Next i
End Sub

It worked! Well, after I used pierre-yves samyn’s answer to figure out the correct RGB value as it wasn’t what the Mac Digital Color Meter was giving me… Thanks so much for your help, @librebel!