Defining your own functions in libreoffice calc

This post will explain how to define your own function in LibreOffice Calc. Your own function can then be used in the same way as the standard functions that LibreOffice comes with.

  • Go to Tools->Macros->Organise Macros->Libreoffice Basic
  • Select Module1 (it is usually already selected)
  • Click Edit

You now have an editor that says something like:

Sub Main
End Sub

Let's define a function called "area" that multiplies two numbers to calculate an area. Type the following below End Sub.

Function Area(width, height)
    Area = width * height
End Function

That's it! You can now use the function Area in your spreadheet!
for example: =AREA(A1,B1)

Here is another example of a function that converts degrees to radians. (Note that a function already exists to do this conversion, it is called RADIANS. But that makes it easy to check that your function works as intended.)

Function Deg2Rad(degrees)
    Deg2Rad = degrees / 180.0 * pi()
End Function

Brilliantly easy!

Just remember the following:

  • Your function cannot refer to specific cells. If you want to give it values to calculate with, they need to be specified between the brackets
  • Do not use the name of a function that already exists.
  • The function always needs a line that assigns a value to the function name. So a function called ABC must have a line that says ABC = x, where x is the value that contains the result of the calculation.
This entry was posted in libreoffice, openoffice. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *