0% found this document useful (0 votes)
155 views

Array List VBA

This document provides a complete guide to using the VBA ArrayList object in Excel macros. It lists common ArrayList tasks like adding, accessing, and removing items and describes the associated method and parameters. Examples are given for each task like using Item to access an item by index, Add to add new items, and Count to get the number of items in the ArrayList.

Uploaded by

Mak Un Nanban
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Array List VBA

This document provides a complete guide to using the VBA ArrayList object in Excel macros. It lists common ArrayList tasks like adding, accessing, and removing items and describes the associated method and parameters. Examples are given for each task like using Item to access an item by index, Add to add new items, and Count to get the number of items in the ArrayList.

Uploaded by

Mak Un Nanban
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

VBA ArrayList - A Complete Guide - Excel Macro Mastery https://excelmacromastery.

com/vba-arraylist/

Task Method Parameters Examples

Access item Item index - long integer value = list.Item(0)


value = list.Item(3)

Access item Item index - long integer value = list.Item(coll.Count - 1)


added last

Access item Item index - long integer value = list.Item(0)


added first

Access all N/A N/A Dim element As Variant


items(For For Each element In fruit
Each) Debug.Print element
Next element

Access all Item index - long integer Dim i As Long


items(For) For i = 0 To list.Count - 1
Debug.Print list.item(i)
Next i

Add item Add object or value list.Add "Apple"


list.Add "Pear"

1 of 3 04-03-2020, 15:05
VBA ArrayList - A Complete Guide - Excel Macro Mastery https://excelmacromastery.com/vba-arraylist/

Task Method Parameters Examples

Copy Clone None Dim list2 As Object


ArrayList to Set list2 = list.Clone
another
ArrayList

Copy to Array ToArray None Dim arr As Variant


arr = list.ToArray

Copy to a ToArray None Sheet1.Range("A1").Resize(1,


range(row) list.Count).Value = list.ToArray

Copy to a ToArray None Sheet1.Range("A3").Resize(list.Count,


range(column) 1).Value =
WorksheetFunction.Transpose(list.ToArray)

Create CreateObject "System.Collections.ArrayList" Dim list As Object


Set list =
CreateObject("System.Collections.ArrayList")

Declare N/A N/A Dim list As Object

Find - check if Contains item to find list.Contains("Apple")


item exists

Find the IndexOf 1. Item to find. Dim index As Long


position of an 2. Position to start searching ' Search from 0 position
item in the from. index = fruit.IndexOf("Pear", 0)
ArrayList

2 of 3 04-03-2020, 15:05
VBA ArrayList - A Complete Guide - Excel Macro Mastery https://excelmacromastery.com/vba-arraylist/

Task Method Parameters Examples

Get number of Count None totalElements = list.Count


items

Insert Item Insert 1. Index - position to insert list.Insert 0, "Peach" ' First
at. list.Insert 1, "Banana" ' Second
2 Value - object or value to list.Insert list.Count, "Orange" ' Last
insert.

Remove all Clear None list.Clear


Items

Remove item RemoveAt Index - position where the list.RemoveAt 0


at position item is

Remove item Remove Item - the item to remove from list.Remove "Apple"
by name the ArrayList

Remove a RemoveRange 1. Index - starting postion. list.RemoveRange 1,3


range of Items 2. Count - the number of items
to remove.

Reverse the Reverse None list.Reverse


list

Sort in Sort None list.Sort


ascending

3 of 3 04-03-2020, 15:05

You might also like