Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use arrays in VBA when there are collections?

Tags:

arrays

excel

vba

many people use extensively arrays in Excel/VBA to store a list of data. However, there is the collection object which in my view is MUCH MUCH more convenient (mainly: don't need to re/define length of the list).

So, I am sincerely asking myself if I am missing something? Why do other people still use arrays to store a list of data? Is it simply a hangover of the past?

like image 266
tyrex Avatar asked May 14 '12 07:05

tyrex


People also ask

What is the difference between collection and array in VBA?

Collections in VBA are objects that can store groups of related items, much like an array. Unlike arrays, a single collection can store items of different types because each item in a collection is stored as a Variant.

What is the use of array in VBA?

In VBA arrays are used to define the group of objects together, there are nine different array functions in VBA and they are ARRAY, ERASE, FILTER, ISARRAY, JOIN, LBOUND, REDIM, SPLIT and UBOUND, all of these are inbuilt functions for array in VBA, Array function gives us the value for the given argument.

Can a collection be an array?

Arrays can hold the only the same type of data in its collection i.e only homogeneous data types elements are allowed in case of arrays. Collection, on the other hand, can hold both homogeneous and heterogeneous elements. Arrays can hold both object and primitive type data.

Are arrays faster in VBA?

Looping through an array is way faster than looking through a range. Which makes looping through an array 96.39% faster than through a loop. The more values you have, the bigger the difference will be.


2 Answers

Several reasons to use arrays instead of collections (or dictionaries):

  • you can transfer easily array to range (and vice-versa) with Range("A1:B12") = MyArray
  • collections can store only unique keys whereas arrays can store any value
  • collections have to store a couple (key, value) whereas you can store whatever in an array

See Chip Pearson's article about arrays for a better understanding

A better question would rather be why people would use collections over dictionaries (ok, collections are standard VBA whereas you have to import dictionaries)

like image 173
JMax Avatar answered Sep 20 '22 05:09

JMax


@CharlesWilliams answer is correct: looping through all the values of an array is faster than iterating a Collection or dictionary: so much so, that I always use the Keys() or Items() method of a dictionary when I need to do that - both methods return a vector array.

A note: I use the Dictionary class far more than I use collections, the Exists() method is just too useful.

There are, or course, drawbacks to collections and dictionaries. One of them is that arrays can be 2- or even 3-Dimensional - a much better data structure for tabulated data. You can store arrays as members of a collection, but there's some downsides to that: one of them is that you might not be getting a reference to the item - unless you use arrItem = MyDictionary(strKey) you will almost certainly get a 'ByVal' copy of the array; that's bad if your data is dynamic, and subject to change by multiple processes. It's also slow: lots of allocation and deallocation.

Worst of all, I don't quite trust VBA to deallocate the memory if I have a collection or dictionary with arrays (or objects!) as members: not on out-of-scope, not by Set objCollection = Nothing, not even by objDictionary.RemoveAll - it's difficult to prove that the problem exists with the limited testing toolkit available in the VBE, but I've seen enough memory leaks in applications that used arrays in dictionaries to know that you need to be cautious. That being said, I never use an array without an Erase command somewhere.

@JMax has explained the other big plus for arrays: you can populate an array in a single 'hit' to the worksheet, and write back your work in a single 'hit.

You can, of course, get the best of both worlds by constructing an Indexed Array class: a 2-dimensional array with associated collection or dictionary objects storing some kind of row identifier as the keys, and the row ordinals as the data items.

like image 44
Nigel Heffernan Avatar answered Sep 21 '22 05:09

Nigel Heffernan