Contents
Index 🔎︎

# ReDim Statement

Declares a variable or an array.

## Syntax:

    [ReDim]Dim VarName [(start To end)] [As VarType][, VarName2 [(start To end)] [As VarType][,...]]

Optionally, you can add the Preserve keyword as a parameter to preserve the contents of the array that is redimensioned.

## Parameters:

VarName: Any variable or array name.

Start, End: Numerical values or constants that define the number of elements (NumberElements=(end-start)+1) and the index range.

Start and End can be numeric expressions if ReDim is used at the procedure level.

VarType: Keyword that declares the data type of a variable.

Keyword: Variable type

Bool: Boolean variable (True, False)

Date: Date variable

Double: Double floating point variable (1.79769313486232x10E308 - 4.94065645841247x10E-324)

Integer: Integer variable (-32768 - 32767)

Long: Long integer variable (-2,147,483,648 - 2,147,483,647)

Object: Object variable (can only be subsequently defined by Set!)

[Single]: Single floating-point variable (3.402823x10E38 - 1.401298x10E-45). If no key word is specified, a variable is defined as Single, unless a statement from DefBool to DefVar is used.

String: String variable containing a maximum of 64,000 ASCII characters.

Variant: Variant variable type (can contain all types and is set by definition).

In LibreOffice Basic, you do not need to declare variables explicitly. However, you need to declare an array before you can use them. You can declare a variable with the Dim statement, using commas to separate multiple declarations. To declare a variable type, enter a type-declaration character following the name or use a corresponding key word.

LibreOffice Basic supports single or multi-dimensional arrays that are defined by a specified variable type. Arrays are suitable if the program contains lists or tables that you want to edit. The advantage of arrays is that it is possible to address individual elements according to indexes, which can be formulated as numeric expressions or variables.

There are two ways to set the range of indices for arrays declared with the Dim statement:

DIM text(20) As String REM 21 elements numbered from 0 to 20

DIM text(5 to 25) As String REM 21 elements numbered from 5 to 25

DIM text$(-15 to 5) As String REM 21 elements (0 inclusive),

rem numbered from -15 to 5

Variable fields, regardless of type, can be made dynamic if they are dimensioned by ReDim at the procedure level in subroutines or functions. Normally, you can only set the range of an array once and you cannot modify it. Within a procedure, you can declare an array using the ReDim statement with numeric expressions to define the range of the field sizes.

## Example:

    Sub ExampleRedim
    Dim iVar() As Integer, iCount As Integer
    ReDim iVar(5) As Integer
    For iCount = 1 To 5
        iVar(iCount) = iCount
    Next iCount
    ReDim iVar(10) As Integer
    For iCount = 1 To 10
        iVar(iCount) = iCount
    Next iCount
    End Sub