You are on page 1of 4

Macro(4)

Backend work by the system for macros

Step1 :Tools  Macros  Organize Macros  OpenOffice Basic

Step 2 : Select the macro  Total Marks  Edit button


The above code is the system generate code for TotalMarks macro.

Use A macro as a function


Step 1 : Children by now, you know how to open the code window. Open the code window.

Step 2: Type the below function in the code window under Main Sub and End Sub. Supporting window is below.
Ex 1. Division of two numbers.

Function DivTwo()
Dim b as integer, c as integer
b=10
c=2
DivTwo=b/c
End function.

Step 3: Click on the save button on the Standard tool bar.


Step 4 : Run the macro in the OpenOffice Calc. Keep your cursor in any cell and type “=DivTwo()”

Step 5 : You can see the answer as 2. Because the values of b and c are 10 and 2.

Ex 2. Multiplication of 3 numbers

Function Multi123()

Dim x as integer, y as integer, z as integer

a=10

b=10

c=10

Multi123=a*b*c

End Function

Children follow the above steps to execute the Multi123() function also.

Ex. 3 Subtraction of 2 numbers

Function Subtra123()

Dim l as integer, m as integer

l= 100

m=50

Subtra123=l-m

End Function

Children follow the above steps and execute the Subtra123() function also.

Children in the above 3 example function the values are directly assigned to the variables like l=100, m=50, a=10,b=10 etc..

But from the below examples we are going to take the values from cells in OpenOffice.org( OOo Calc)

Ex. 4. Sum123() function by referring the value from the cell


Function Sum123()

Dim TheSum as Double

Dim oSheet

Dim oCell1

Dim oCell2

oSheet = ThisComponent.CurrentController.ActiveSheet

oCell1= oSheet.getCellByPosition(0,1) ‘ oCell1 will goto the A2 cell in SpreadSheet.

oCell2= oSheet.getCellByPosition(0,2) ‘ oCell2 will goto the A3 cell in SpreadSheet.

TheSum=oCell1.getValue() + oCell2.getValue()

Sum123=TheSum

End Function

Explanation : Here in the above function,

1) oSheet, oCell1, oCell2 are just variables. Just like we have taken a,b,c,l,m in the above examples.
2) (0,0),(0,1) etc.. are given in the table above. (0,0) means A1 in SpreadSheet. (1,1) means B1 in
SpreadSheet. Learn all
3) Whatever the sheet (Sheet1, Sheet2 etc..) we select children, that sheet will become the active
Sheet for that we have the code as “ ThisComponent.CurrentController.ActiveSheet”
4) getCellByPosition() function is used to goto that particular cell. Children remember the first letter
of the first word is small and first letter of the remaining words will be capital. That is rule. You must
write the function getCellByPosition() as it is.
5) getValue() function is used to take the value from that particular cell after going to the cell.

Ex.5

Function Division123()

Dim oSheet

Dim oCell1

Dim b

oSheet = ThisComponent.CurrentController.ActiveSheet

oCell1 = oSheet.getCellByPosition(0,3)

b=2

Division123=oCell1.getValue()/b

End Function

Do the above macro function also.

You might also like