I have a range of cells named, with not contiguous cells (NOT rectangular range)
I need to assign a background value and color to each cell in this range.
In the macro, naming works. Is it possible to assign a background value and color to all cells in the range in one statement?
Otherwise, if I have to make a loop, how to write it? (what I wrote does not work:
Error PROPERTY OR METHOD NOT FOUND: GETROWS
Thank you for your help
sub addColorToNameGameBoard()
Dim myDocument As Object
Dim zoneOfNames As Object
Dim gameRangeName As String
Dim gameRangeNameCoordinates As String
Dim oCellAdress As New com.sun.star.table.CellAddress
myDocument = Thiscomponent
gameRangeNameCoordinates = "Feuille1.$B$5:$C$7~Feuille1.$D$4:$H$8"
gameRangeName = "plateauDeJeu"
zoneOfNames = myDocument.NamedRanges
If zoneOfNames.hasByName(gameRangeName) then
zoneOfNames.removeByName(gameRangeName)
end If
zoneOfNames.addNewByName(gameRangeName, gameRangeNameCoordinates ,oCellAdress,0)'
Dim myGameBoard as Object
myGameBoard = zoneOfNames.getByName(gameRangeName)
'KO myGameBoard.BackColor = rgb(255,0,0)
'myGameBoard.CellBackColor = RGB(100, 0 ,100)
Dim ReferredCells as Object
Dim aRangeAddress as Object
oReferredCells = myGameBoard.getReferredCells()
aRangeAddress = oReferredCells.getRangeAddress()
Dim oSheet As Object
oSheet = myDocument.getSheets().getByName("Feuille1") 'Sheet1
Dim oCellRange As Object
oCellRange = oSheet.getCellRangeByName(gameRangeName)
Dim myCell as Object
Dim i as long, j as long
For i = 0 To oCellRange.getRows().getCount()-1
For j = 0 To oCellRange.getColumns().getCount()-1
myCell = oCellRange.getCellByPosition(i,j)
myCell.setValue(4)
myCell.cellbackcolor = RGB(50,60,70)
Next j
Next i
End Sub
I tried different syntax to get number of rows and columns, nothing worked
Changing the color of an entire range in one line is not difficult: specify the name of some style with the design you want for the range's
.CellStyleproperty, and the entire range will be instantly redrawn. The situation is a little worse with assigning a certain value to each cell. As explained in this answer - Libreoffice calc - how to write a same value into a range, you will have to use row and column loops. And the situation is very bad with the ranges that you described using the tilde - there are no built-in tools for processing such constructions. This means that you will have to process each part of the address separately. In general, the solution code could look like this: