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

Dynamically Populating VBA Array Variables To Store Lists of Data

This document discusses three methods for dynamically populating VBA array variables to store lists of data. The first method resizes the array before adding values. The second method resizes the array as each new value is added using ReDim Preserve. The third method creates the array from a delimited string, where values are separated by a designated character. Arrays allow storing multiple values in a single variable like a mini-spreadsheet. Reference numbers correspond to data locations within the array.

Uploaded by

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

Dynamically Populating VBA Array Variables To Store Lists of Data

This document discusses three methods for dynamically populating VBA array variables to store lists of data. The first method resizes the array before adding values. The second method resizes the array as each new value is added using ReDim Preserve. The third method creates the array from a delimited string, where values are separated by a designated character. Arrays allow storing multiple values in a single variable like a mini-spreadsheet. Reference numbers correspond to data locations within the array.

Uploaded by

MC
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data

HOME ABOUT BLOG VBA VAULT STORE CONTACT

November 12, 2016


Search
Dynamically
Populating VBA Latest Blog Posts

Array Variables List of the World Countries,


Abbreviations, & Capitals

To Store Lists of Mar 15, 2023

Data Printable Blank Calendar


Templates For 2023
Chris Newman
Jan 5, 2023

Add Total Value Labels to Stacked


Bar Chart in Excel (Easy)
Oct 30, 2022

How To Build Waterfall (Bridge)


Charts In Excel
What Are Arrays? Oct 28, 2022

Arrays are a variant type variable


that you can use in VBA coding to
store a list of data. Think of it as a
mini-spreadsheet inside of a single
variable. You store data into an
array by referring to a reference
number that corresponds with the
location that the piece of data is
positioned in.
https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 1/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data

Below is an example of an array


that is holding all the month names
within a year. Notice that the
reference number starts at zero
instead of one.

You can also have multi-


dimensional arrays. Below is a two-
dimensional array that looks much
like a typical spreadsheet is setup.
You can create even more
dimensions if you need to (ie think
"Data Cube"), but in my experience
two dimensions is the max that a
typical person will ever need. 

Now that you've had a quick


overview of what arrays are, let's
get into the meat of this article and
learn various ways to dynamically
resize these variables to fit all of
your data.

Method 1: Resizing
First

https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 2/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data

This method resizes the array to


the size of your target data or list
before actually placing the values
inside the array. This can be a good
option if you know beforehand
how many items you need to store.

Sub PopulatingArrayVariable()
'PURPOSE: Dynamically Create Array
Variable based on a Given Size

Dim myArray() As Variant


Dim DataRange As Range
Dim cell As Range
Dim x As Long

'Determine the data you want stored


  Set DataRange =
ActiveSheet.UsedRange

'Resize Array prior to loading data


  ReDim
myArray(DataRange.Cells.Count)

'Loop through each cell in Range and


store value in Array
  For Each cell In DataRange.Cells
    myArray(x) = cell.Value
    x = x + 1
  Next cell

'Print values to Immediate Window


(Ctrl + G to view)
  For x = LBound(myArray) To
UBound(myArray)
    Debug.Print myArray(x)
  Next x

End Sub

Method 2: Resizing
As You Go
This VBA method resizes the array
variable right before storing each
item. The key command here is
"Preserve". This tells the code to

https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 3/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data

keep all the stored items in the


Array while increasing it's storage
capacity. If you forgo having
Preserve in your code immediately
after you ReDim, the array variable
will wipe clean of data previously
stored in it before re-dimensioning.

Sub PopulatingArrayVariable()
'PURPOSE: Dynamically Create Array
Variable based on a Given Size

Dim myArray() As Variant


Dim DataRange As Range
Dim cell As Range
Dim x As Long

'Determine the data you want stored


  Set DataRange =
ActiveSheet.UsedRange

'Loop through each cell in Range and


store value in Array
  For Each cell In DataRange.Cells
    ReDim Preserve myArray(x)
    myArray(x) = cell.Value
    x = x + 1
  Next cell

'Print values to Immediate Window


(Ctrl + G to view)
  For x = LBound(myArray) To
UBound(myArray)
    Debug.Print myArray(x)
  Next x

End Sub

Method 3: Creating
From A Delimited
String
Another way you can populate an
array variable is through a
delimiter. A delimiter is simply a

https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 4/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data

designated set of character(s) that


separates out your values. Have
you ever heard of a CSV file? CSV
stands for "Comma-Separated
Values" where a comma symbol
tells your computer how to
separate each value apart from one
another.

You can use this same concept to


make your own delimited string or
(more realistically) read a delimited
string exported from your database
software to populate the array. In
my below example code I am
taking a set range and turning it
into a delimited string with the
characters ";|;" separating each
value. 

Sub PopulatingArrayVariable()
'PURPOSE: Dynamically Create Array
Variable based on a Given Size

Dim myArray As Variant


Dim myString As String
Dim DataRange As Range
Dim cell As Range
Dim x As Long

'Determine the data you want stored


  Set DataRange =
ActiveSheet.Range("A1:A100")

'Loop through each cell in Range and


store value in delimited string
  For Each cell In DataRange.Cells
    myString = myString & ";|;" &
cell.Value
  Next cell

'Remove first delimiter from string


(;|;)
  myString = Right(myString,
Len(myString) - 3)
  
'Create an array with the Split()
https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 5/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data
function
  myArray = Split(myString, ";|;")

'Print values to Immediate Window


(Ctrl + G to view)
  For x = LBound(myArray) To
UBound(myArray)
    Debug.Print myArray(x)
  Next x

End Sub

If you are already starting with a


delimited string, use this simplified
VBA code to accomplish the same
thing.

Sub PopulatingArrayVariable()
'PURPOSE: Dynamically Create Array
Variable based on a Given Size

Dim myArray As Variant


Dim myString As String
Dim x As Long

'Store delimited string to a


variable
  myString =
"Apple;|;Orange;|;Pear;|;Peach;|;Grapes"
  
'Create an array with the Split()
function
  myArray = Split(myString, ";|;")

'Print values to Immediate Window


(Ctrl + G to view)
  For x = LBound(myArray) To
UBound(myArray)
    Debug.Print myArray(x)
  Next x

End Sub

Method 4: Pulling
From An Excel
Table [My Favorite
Option!]
https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 6/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data

This is my favorite way to populate


array variables dynamically
because it has a user-interface
aspect to it (ie it allows you to
make changes to the inputs
without rewriting the code). Now
you may be thinking to yourself
that a couple of the VBA snippets
above are pulling from ranges that
you could easily substitute with a
named range in your code. This is
true, but tables have the auto-
expanding feature that can make
you 100% certain from a visual
perspective that you are picking up
all your values.

You will want to note that when


you size an Array variable from a
Range or Table, the first reference
number will be a One instead of
the typical Zero.

Sub PopulatingArrayVariable()
'PURPOSE: Dynamically Create Array
Variable based on a Single Columned
Table

Dim myArray() As Variant


Dim TempArray() As Variant
Dim myTable As ListObject
Dim x As Long

'Set path for Table variable


  Set myTable =
ActiveSheet.ListObjects("Table1")
  
'Create Array List from Table
  TempArray =
myTable.DataBodyRange.Columns(1)
  
'Convert from vertical to horizontal
array list
  myArray =
https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 7/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data
Application.Transpose(TempArray)

'Loop through each item in the Table


Array (displayed in Immediate Window
[ctrl + g])
  For x = LBound(myArray) To
UBound(myArray)
    Debug.Print myArray(x)
  Next x

End Sub

Any Other
Methods?
Are there any other methods you
use to populate arrays
dynamically? I would love to hear
from you in the comments section
below and maybe I can add some
other options to the ones I've
already discussed in the article.

About The Author


Hey there! I’m Chris and I run
TheSpreadsheetGuru website in my
spare time. By day, I’m actually a
finance professional who relies on
Microsoft Excel quite heavily in the
corporate world. I love taking the
things I learn in the “real world”
and sharing them with everyone
here on this site so that you too
can become a spreadsheet guru at
your company.
https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 8/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data

Through my years in the corporate


world, I’ve been able to pick up on
opportunities to make working
with Excel better and have built a
variety of Excel add-ins, from
inserting tickmark symbols to
automating copy/pasting from
Excel to PowerPoint. If you’d like to
keep up to date with the latest
Excel news and directly get emailed
the most meaningful Excel tips I’ve
learned over the years, you can
sign up for my free newsletters. I
hope I was able to provide you
with some value today and I hope
to see you back here soon!

- Chris
Founder, TheSpreadsheetGuru.com

 26 Comments 4 Likes Share

 USE YAHOO! FI PREVENT MICROSO

https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 9/10
20/4/23, 9:25 Dynamically Populating VBA Array Variables To Store Lists of Data

27 Comments 
1 Login

Join the discussion…

LOG IN WITH

OR SIGN UP WITH DISQUS ?

Name

 6 Share

Best Newest Oldest

Affiliate Program | About | Privacy Policy

Guru Solutions, LLC | © 2015-2023 | ALL RIGHTS RESERVED


Excel, PowerPoint, Word, & the rest of the Office Suite are registered trademarks of the Microsoft
Corporation
This site is not affiliated with Microsoft Corporation.

https://www.thespreadsheetguru.com/blog/dynamically-populating-array-vba-variables 10/10

You might also like