Calc Guide
which is more cumbersome than treating it as an array with two
dimensions as is done in Listing 5.
Listing 8. Add cell A2:C5 in every sheet
Function SumCellsAllSheets()
Dim TheSum As Double
Dim iRow As Integer, iCol As Integer, i As Integer
Dim oSheets, oSheet, oCells
Dim oRow(), oRows()
oSheets = ThisComponent.getSheets()
For i = 0 To oSheets.getCount() - 1
oSheet = oSheets.getByIndex(i)
oCells = oSheet.getCellRangeByName("A2:C5")
REM getDataArray() returns the data as variant so strings
REM are also returned.
REM getData() returns data data as type Double, so only
REM numbers are returned.
oRows() = oCells.getData()
For iRow = LBound(oRows()) To UBound(oRows())
oRow() = oRows(iRow)
For iCol = LBound(oRow()) To UBound(oRow())
TheSum = TheSum + oRow(iCol)
Next
Next
Next
SumCellsAllSheets = TheSum
End Function
Tip
When a macro is called as a Calc function, the macro cannot
modify any value in the sheet from which the macro was called.
Sorting
Consider sorting the data in Figure 288. First, sort on column B
descending and then column A ascending.
Figure 303: Sort column B descending and column A ascending
354 OpenOffice.org 3.x Calc Guide