Page 238 - TP_IT_V1.0_C10_flipbpookl
P. 238
For example, if you want to sets the content of cell A1 in the active spreadsheet to “Hello, World!”.
Sub SetCellValue()
Dim oSheet As Object
Dim oCell As Object
' Get the active sheet
oSheet = ThisComponent.CurrentController.ActiveSheet
' Get the cell A1
oCell = oSheet.getCellRangeByName("A1")
' Set the value of the cell
oCell.String = "Hello, World!"
End Sub
In the give function, the ThisComponent.CurrentController.ActiveSheet is used to access the current sheet
and getCellRangeByName(“A1”) retrieves the cell with the specified name “A1” from the current spreadsheet.
When you run the SetCellValue macro, you should see that cell A1 now contains the text Hello, World!.
Sorting a Column Using a Macro
Sorting means arranging the data in the ascending or descending order. To sort data in a spreadsheet select the range
of cells and click on Data → Sort.
These steps to sort a specific range of cells can be stored in a macro. So that next time if you wish to sort the data
in any of the sheets you just have to run the macro and the sorting will be done automatically. Take care that the
range of cells used at the time of creating a macro are matching with the range of cells you wish to sort using a
macro. For example, you created the macro for range B5:B20 will cover 16 consecutive cells. So at the time of using
the macro total 16 consecutive cells should be selected to run the macro otherwise it will not work and data will not
be sorted.
Let us take the example of the given name of the cites to be sorted in the ascending order by creating a macro as
shown below:
Step 1: Create a spreadsheet with the given list of the cities in cell A1:A21.
236 Trackpad Information Technology (Ver. 1.0)-X

