MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Option Base


Option Base is used to declare the default lower bound of array elements. It is declared at module level and is valid only for the current module.

By default (and thus if no Option Base is specified), the Base is 0. Which means that the first element of any array declared in the module has an index of 0.

If Option Base 1 is specified, the first array element has the index 1

Example in Base 0 :


Option Base 0
Sub BaseZero()
 Dim myStrings As Variant

 ' Create an array out of the Variant, having 3 fruits elements
 myStrings = Array("Apple", "Orange", "Peach")

 Debug.Print LBound(myStrings) ' This Prints "0"
 Debug.Print UBound(myStrings) ' This print "2", because we have 3 elements beginning at 0 ->
0,1,2

 For i = 0 To UBound(myStrings)

 Debug.Print myStrings(i) ' This will print "Apple", then "Orange", then "Peach"

 Next i
End Sub

Same Example with Base 1


Option Base 1
Sub BaseOne()
 Dim myStrings As Variant

 ' Create an array out of the Variant, having 3 fruits elements
 myStrings = Array("Apple", "Orange", "Peach")

 Debug.Print LBound(myStrings) ' This Prints "1"
 Debug.Print UBound(myStrings) ' This print "3", because we have 3 elements beginning at 1 ->
1,2,3

 For i = 0 To UBound(myStrings)

 Debug.Print myStrings(i) ' This triggers an error 9 "Subscript out of range"

 Next i
End Sub

The second example generated a Subscript out of range (Error 9) at the first loop stage because an attempt to access the index 0 of the array was made, and this index doesn't exists as the module is declared with Base 1

The correct code with Base 1 is :



 For i = 1 To UBound(myStrings)

 Debug.Print myStrings(i) ' This will print "Apple", then "Orange", then "Peach"

 Next i

It should be noted that the Split function always creates an array with a zero-based element index regardless of any Option Base setting. Examples on how to use the Split function can be found here

Split Function
Returns a zero-based, one-dimensional array containing a specified number of substrings.

In Excel, the Range.Value and Range.Formula properties for a multi-celled range always returns a 1-based 2D Variant array.

Likewise, in ADO, the Recordset.GetRows method always returns a 1-based 2D array.

One recommended 'best practice' is to always use the LBound and UBound functions to determine the extents of an array.

'for single dimensioned array
Debug.Print LBound(arr) & ":" & UBound(arr)
Dim i As Long
For i = LBound(arr) To UBound(arr)
 Debug.Print arr(i)
Next i
'for two dimensioned array
Debug.Print LBound(arr, 1) & ":" & UBound(arr, 1)
Debug.Print LBound(arr, 2) & ":" & UBound(arr, 2)
Dim i As long, j As Long
For i = LBound(arr, 1) To UBound(arr, 1)
 For j = LBound(arr, 2) To UBound(arr, 2)
 Debug.Print arr(i, j)
 Next j
Next i

The Option Base 1 must be at the top of every code module where an array is created or re-dimensioned if arrays are to be consistently created with an lower boundary of 1.

Conclusion

In this page (written and validated by ) you learned about VBA Option Base . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Declaring Variables.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.