Macros/Calc/ba019/en

Summary
This page provides a LibreOffice Basic macro procedure that creates a new named range referencing $Sheet1.$B$3:$D$6. It is an example drawn from Chapter 13 ("Calc as a Database") of the 7.0 Calc Guide.

Description
In a macro, a named range is accessed, created, and deleted using the NamedRanges service of a Calc document. This service has a number of methods associated with it, the following of which are particularly useful for creating named ranges:


 * getByName(Name)
 * Returns the range or expression with the specified name.


 * getElementNames
 * Returns an array of all named ranges in the current document.


 * hasByName(Name)
 * Returns a boolean: TRUE if a range with this name is in the current document, and FALSE otherwise.


 * addNewByName(Name, Content, Position, Type)
 * Adds a new named range to the current document. This method has four arguments:
 * Name – A string that contains the name of the new range.
 * Content – A string that contains the range address or formula expression being named.
 * Position – The base address for relative cell references.
 * Type – A combination of flags that specify the type of named range being defined. These flags are listed in the following table. This parameter defaults to zero for any common named range.

As an example, the following code uses the above methods to check if a range named MyNRange exists. If it does not exist, then the macro creates a range with the name and sets it to access the cell range B3:D6.

Code
This [[Media:Create_named_range_with_macro.ods|Calc spreadsheet]] contains the above LibreOffice Basic code.