Which Excel objects are zero-based and which are one-based?
Using VBA to access the first sheet in a worksheet is Worksheets(1). The first item in a ListBox is myListBox.List(0). I heard that Collections are 1-based but I don't know what they are. VBA arrays are 0-based. Excel string functions like MID are 1-based. Is there a general principle as to what is based on 0 or 1, or could you provide a list of each?
There are 3 main types of grouping constructs available in VBA, with distinctions between indexes
-
Collections - 1-based index
- 0-based exceptions: UserForm collections like Tabs, Pages, Controls (ListBox, TextBox)
- Collections are native Excel objects that contain groups (or lists) of logically related objects
- Normally used to hold complex objects, but can hold basic types as well
-
Excel collections:
- Workbooks, Sheets, Ranges, Shapes
- Sheets(1) is the first in the file, Cells(1, 1) is the cell in first Row and first Column
-
Main advantage of collections is the convenience of accessing elements by name
- For-Each loop is very efficient (compared to For-Each processing of arrays)
- Accessing individual items by index though, is faster than accessing them by name
-
Arrays - 0-based by default, but the first index can be changed to any number (illustrated bellow)
- Arrays are variables that contain a set of related variables
- Normally used for primitive data types like Boolean, Integer, Long, String, Double, etc
-
Once it is defined it will hold only one type of items:
Dim x() As Long
- To hold more complex objects an array can be defined as
Dim x() As Variant
-
Variants can be any type of Objects, including Workbooks, Sheets, Ranges, Arrays
Dim x As Variant: x = Array(1) '1 Variant variable containing 1 array
Dim y(2) As Variant '1 Variant array containing 3 arrays
y(0) = Array(1): y(1) = Array(2): y(2) = Array(3)
- To hold more complex objects an array can be defined as
-
Main advantage of arrays is performance when accessing items by index
-
For index=0 To 10
loops are faster thanFor-Each
loops
-
-
Dictionaries - not indexed, but indexes can be simulated with Keys
- Native to VB Script, not VBA (must use an external Library)
- Can hold any type of objects including Arrays, Collections, or other Dictionaries
A ListBox is a complex object and can be accessed through the 0-based collection of Controls
The .List() property of ListBox is a 0-based array
Other notes
0-based indexes are the standard for other languages
-
VBA introduced the 1-based concept to make it more intuitive for new users:
- Sheet1 to Sheet3, with collection Count of 3 easier to use than
- Sheet0 to Sheet2, with collection Count of 3
Some practical examples of the difference between their indexes:
Public Sub vbaCollections()
Dim c As New Collection '1-based index
c.Add Item:="a", Key:="1" 'index 1; Key must a String
c.Add Item:="b", Key:="2" 'index 2
c.Add Item:="c", Key:="3" 'index 3
Debug.Print c.Count '3; Items in index sequence: a,b,c, Keys: "1","2","3"
Debug.Print c.Item(1) 'a; not available for Dictionaries
'Debug.Print c.Key("1") 'invalid, so is: c.Key(1)
c.Remove Index:=2
Debug.Print c.Count '2; items in index sequence: a,c, Keys: "1","3"
'c.Remove Item:="c" 'invalid, so is: c.Remove Key:="3"
'c.Add Item:="c", Key:="3", Before:=1 'Key must be unique - Error
c.Add Item:="c", Key:="5", Before:=1 'allows duplicate Item
Debug.Print c.Count '3; items in index sequence: c,a,c, Keys: "5","1","3"
End Sub
Public Sub vbaArrays()
Dim a() As Long, b(3) As Long 'Arrays default to "Option Base {0 | 1}"
Dim c(0 To 0) 'if "Option Base" not defined, it defaults to 0
Dim ar(1) As Worksheet: Set ar(0) = Worksheets(1) 'array with 1 Worksheets object
ReDim a(3) 'creates an array of 4 elements; indexes 0,1,2,3
Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a) 'LB: 0, UB: 3
Debug.Print UBound(a) - LBound(a) '3, array b() is the same
'even whith "Option Base 1", the following still default to 0
Dim v As Variant: v = Split("A B") 'array with 2 items: v(0) = "A", v(1) = "B"
'UserForm1.ListBox1.List = Array("Test") 'array with 1 item: .List(0,0) = "Test"
ReDim a(0 To 3) 'creates an array of 4 elements; indexes 0,1,2,3
a(0) = 1: a(1) = 2: a(2) = 3 'a(3) defaults to 0
Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a) 'LB: 0, UB: 3
Debug.Print UBound(a) - LBound(a) '3; offset index by -1
ReDim a(1 To 3) 'creates an array of 3 elements; indexes 1,2,3
a(1) = 1: a(2) = 2: a(3) = 3
Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a) 'LB: 1, UB: 3
Debug.Print UBound(a) - LBound(a) '2; offset count by +1
ReDim a(5 To 7) 'creates an array of 3 elements; indexes 5,6,7
a(5) = 1: a(6) = 2: a(7) = 3
Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a) 'LB: 5, UB: 7
Debug.Print UBound(a) - LBound(a) '2; offset count by +1
ReDim a(-3 To -1) 'creates an array of 3 elements; indexes -3,-2,-1
a(-3) = 1: a(-2) = 2: a(-1) = 3
Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a) 'LB: -3, UB: -1
Debug.Print UBound(a) - LBound(a) '2; offset count by +1
End Sub
Public Sub vbsDictionaries()
Dim d As Object 'not indexed (similar to linked lists)
Set d = CreateObject("Scripting.Dictionary") 'native to VB Script, not VBA
d.Add Key:="a", Item:=1 'index is based on Key (a, b, c)
d.Add Key:="b", Item:=2
d.Add Key:="c", Item:=3
Debug.Print d.Count '3; Keys: a,b,c, Items: 1,2,3
Debug.Print d(1) 'output is empty ("") - adds new element: Key:="1", Item:=""
Debug.Print d.Count '4; Keys: a,b,c,1, Items: 1,2,3,Empty
Debug.Print d("a") '1
Debug.Print d(1) 'output is Empty ("") from element with Key:="1"
'd.Add Key:="b", Item:=2 'attempt to add existing element: Key:="b" - Error
'd.Keys - 0-based array (not available for Collections)
'd.Items - 0-based array (not available for Collections)
d.Remove d.Keys()(1) 'remove element Item:=2 (Key:="b")
Debug.Print d.Count '3; Keys: a,c,1, Items: 1,3,""
d.Remove d.Items()(0) 'remove Items element 0 (Key:="1", Item:="")
Debug.Print d.Count '2; Keys: a,c, Items: 1,3
d.Remove "c" 'remove element Key:="c" (Item:=3)
Debug.Print d.Count '1; Keys: a, Items: 1
d.Add Key:="c", Item:=3
Debug.Print d.Count '2; Keys: a,c, Items: 1,3
'd.Remove d.Items()(0) 'invalid
Debug.Print d.Items()(d.Count - 1) '3
d.Remove d.Keys()(d.Count - 1) 'remove last element; access last Key by Key
Debug.Print d.Count '1; Keys: a, Items: 1
Debug.Print d.Exists("a") 'True (not available for Collections)
Debug.Print d.Exists(2) 'False
End Sub
Further reading:
- Arrays vs Collections vs Dictionary
- What Is a Dictionary