EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Array Functions VBA ArrayList
Secondary Sidebar
VBA Array Functions
  • VBA Arrays
    • VBA Arrays
    • VBA ReDim
    • VBA Dim
    • VBA Sort
    • VBA Array Length
    • VBA ArrayList
    • VBA UBound
    • VBA Join
    • VBA Collection
    • VBA Declare Array
    • VBA ReDim Array
    • VBA Array Length
    • VBA Dynamic Array
    • VBA Filter
    • VBA Lbound

VBA ArrayList

By Madhuri ThakurMadhuri Thakur

VBA ArrayList

Excel VBA ArrayList

Data structures are used to store a series of data in programming languages. It binds to the memory rather than address. An ArrayList is one of the data structures in excel. Comparing to normal arrays in excel ArrayList is dynamic. Therefore, no initial declaration of size is needed. ArrayList is not a part of VBA it is associated with an external library which can be used with VBA.

ArrayList can be defined as a list of a nearby memory location. Where the values are retrieved using the index numbers. The list starts from an index number ‘0’, the first element will be inserted into the ‘0’ index and rest is followed by 1, 2, 3, etc. ArrayList offers plenty of built-in operations, sorting, adding, removing, reversing, etc. are some among them.

Adding the Library

To use the ArrayList into the VBA it needs to include the library ‘mscorlib.dll’ which comes with .NET framework.

  • Press F11 or right-click the sheet name to get the code window. Go to the VBA code window, from the main menu select Tools.

VBA ArrayList Example 1-1

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

  • The tools menu contains ‘references’ option and it consists of a list of libraries which supports VBA for including different objects. Click on the Reference option.

VBA ArrayList Example 1-2

  • It will lead you to a window with a list of different libraries which supports in VBA and Excel. Scroll down to find the ‘dll’. Tick mark to confirm the selection then press ‘OK’ button.

VBA ArrayList Example 1-3

Now the library is included in your VBA code and it will support different methods associated with an ArrayList.

How to Create VBA ArrayList in Excel?

Below are the different examples to create VBA ArrayList in Excel.

You can download this VBA ArrayList Excel Template here – VBA ArrayList Excel Template

Excel VBA ArrayList – Example #1

How to Add Values to the ArrayList using VBA?

ArrayList act as a list where we can add values. This will automatically store in the different portions starting from 0,1, 2, etc. The values can add or insert to the ArrayList using the add method.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,580 ratings)

In this example, you will learn how to add a list of values into an ArrayList. Follow the below steps to add ArrayList using VBA Code in excel.

Step 1: To add a list of values to an ArrayList create a function arraylist1.

Code:

Private Sub arraylist1()

End Sub

VBA ArrayList Example 1-4

Step 2: Now we want to include the ArrayList into the function as an object where a list is declared as an ArrayList.

Code:

Private Sub arraylist1()

Dim alist As ArrayList

End Sub

VBA ArrayList Example 1-5

Step 3: Since this is an object to use it, you have to create an instance of the ArrayList. Set a new instance for this object.

Code:

Private Sub arraylist1()

Dim alist As ArrayList
Set alist = New ArrayList

End Sub

VBA ArrayList Example 1-6

Step 4: Now using the ‘Add’ property of an ArrayList adds the values to the ArrayList. Where the list is added into the index values in an order 0,1,2,3 etc.

Code:

Private Sub arraylist1()

Dim alist As ArrayList
Set alist = New ArrayList
alist.Add "192" 'index(0)
alist.Add "168" 'index(1)
alist.Add "1" 'index(2)
alist.Add "240" 'index(3)

End Sub

VBA ArrayList Example 1-7

Step 5: To check whether the values got added into the list, let’s print the array values using a message box. To print the values each index is printed since the values are stored in these partitions.

Code:

Private Sub arraylist1()

Dim alist As ArrayList
Set alist = New ArrayList
alist.Add "192" 'index(0)
alist.Add "168" 'index(1)
alist.Add "1" 'index(2)
alist.Add "240" 'index(3)
MsgBox ("\\" & alist(0) & "." & alist(1) & "." & alist(2) & "." & alist(3))

End Sub

VBA ArrayList Example 1-8

Step 6: Press F5 or run button to run the program and the values will be printed as below. Here an IP address is stored in the ArrayList and while printing the values extra notations are concatenated to form the IP address in a proper format.

Result of Example 1-9

Automation error in VBA

It is a common error happens while running an ArrayList. An automation error may encounter ‘Run-time Error ‘-2146232576 (80131700) Automation Error’

Automation Error

This is because of not the correct version of the .NET framework installed. To work with ArrayList you must have minimum .NET 3.5 or the higher versions of .NET framework.

Excel VBA ArrayList – Example #2

Sorting ArrayList Using VBA Code 

ArrayList supports different functions like sorting, reversing, etc. this help to sort the values inserted into an ArrayList. Once you add a list into the ArrayList it is possible to reverse the inserted list.

Follow the below steps to sort the ArrayList using VBA Code:

Step 1: Create a function called arraysort1 to perform the sorting within the inserted values into an ArrayList.

Code:

Sub arraysort1()

End Sub

VBA ArrayList Example 2-1

Step 2: Declare an object ‘arraysort’ of the ArrayList. Use this object to add and sort the values within the ArrayList.

Code:

Sub arraysort1()

Dim arraysort As ArrayList

End Sub

VBA ArrayList Example 2-2

Step 3: Similar to the first example need to create a new instance of the declared object. Set this instance as a new ArrayList.

Code:

Sub arraysort1()

Dim arraysort As ArrayList
Set arraysort = New ArrayList

End Sub

VBA ArrayList Example 2-3

Step 4: Now using the ‘Add’ method insert the elements to the ArrayList. Which is not possessing any order on values. Randomly inserted some values into the list.

Code:

Sub arraysort1()

Dim arraysort As ArrayList
Set arraysort = New ArrayList
arraysort.Add "13"
arraysort.Add "21"
arraysort.Add "67"
arraysort.Add "10"
arraysort.Add "12"
arraysort.Add "45"

End Sub

VBA ArrayList Example 2-4

Step 5: To note the difference in the ArrayList, let’s print the ArrayList after inserting the values and before sorting it.

Code:

Sub arraysort1()

Dim arraysort As ArrayList
Set arraysort = New ArrayList
arraysort.Add "13"
arraysort.Add "21"
arraysort.Add "67"
arraysort.Add "10"
arraysort.Add "12"
arraysort.Add "45"

MsgBox (arraysort(0) & vbCrLf & arraysort(1) _
& vbCrLf & arraysort(2) & vbCrLf & arraysort(3) _
& vbCrLf & arraysort(4) & vbCrLf & arraysort(5))

End Sub

VBA ArrayList Example 2-5

Step 6: Press F5 on the keyboard or run button on the code window to run the program to print the ArrayList. The ArrayList is printed in the same order as it is inserted since we use the index numbers in its correct order.

Result of Example 2-6

Step 7: Now to this list apply the sort property of the ArrayList. Use the sort method to sort the inserted list. The sort property will sort the list of values in ascending order by default.

Code:

Sub arraysort1()

Dim arraysort As ArrayList
Set arraysort = New ArrayList
arraysort.Add "13"
arraysort.Add "21"
arraysort.Add "67"
arraysort.Add "10"
arraysort.Add "12"
arraysort.Add "45"

arraysort.Sort

MsgBox (arraysort(0) & vbCrLf & arraysort(1) _
& vbCrLf & arraysort(2) & vbCrLf & arraysort(3) _
& vbCrLf & arraysort(4) & vbCrLf & arraysort(5))

End Sub

VBA ArrayList Example 2-7

Step 8: Hit F5 or Run button under VBE to run this code, Where the values are sorted and printed in order from smallest value to largest.

Result of Example 2-8

Excel VBA ArrayList – Example #3

Reversing the ArrayList using VBA Code

When you want to reverse the order of inserted values in an ArrayList reverse method is available. This will reverse the order of the list from its current order. Now we have already sorted the ArrayList in the previous example, which is in ascending order.

Let’s try to reverse the sorted array to make it descending order. Use the reverse method of ArrayList to do this.

Code:

Sub arraysort2()

Dim arraysort As ArrayList
Set arraysort = New ArrayList
arraysort.Add "13"
arraysort.Add "21"
arraysort.Add "67"
arraysort.Add "10"
arraysort.Add "12"
arraysort.Add "45"
arraysort.Sort

arraysort.Reverse

MsgBox (arraysort(0) & vbCrLf & arraysort(1) _
& vbCrLf & arraysort(2) & vbCrLf & arraysort(3) _
& vbCrLf & arraysort(4) & vbCrLf & arraysort(5))

End Sub

ArraySort.Reverse Example 3-1

After applying the reverse method, the ArrayList will become in descending order and use the message box to print the reversed array. The sorted list is changed from large value to small value.

Result of Example 3-2

Things to Remember

  • ArrayList is dynamic in nature; it does not require re-initialization.
  • Different built-in methods are associated with ArrayList.
  • Compared to the array, ArrayList is easy to use in Excel VBA.
  • The supporting .NET libraries should be included in the VBA to work with ArrayList.
  • ArrayList is a continuing memory location which identified using index values.

Recommended Articles

This is a guide to VBA ArrayList. Here we discuss how to create ArrayList in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Arrays
  2. VBA Sort
  3. VBA XML
  4. VBA Month
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more