Vba
Excel VBA Subroutine: How to Call Sub in VBA with Example
What is a Subroutine in VBA? A Subroutine in VBA is a piece of code that performs a specific task...
An array is defined as a memory location capable of storing more than one value. The values must all be of the same data type. Let's say you want to store a list of your favourite beverages in a single variable, you can use VBA array to do that.
By using an array, you can refer to the related values by the same name. You can use an index or subscript to tell them apart. The individual values are referred as the elements of the Excel VBA array. They are contiguous from index 0 through the highest index value.
This tutorial assumes you are using Microsoft Excel version 2013. The knowledge still applies to other versions of Microsoft Excel as well.
In this VBA Programming tutorial, you will learn-
The following are some of the benefits offered by VBA array function
VBA supports two types of arrays namely;
For Example: Dim ArrayMonth(12) As String
For Example: Dim ArrayMonth() As Variant
Syntax to declare arrays
Static arrays
The syntax for declaring STATIC arrays is as follows:
Dim arrayName (n) as datatype
HERE,
| Code | Action |
| Dim arrayName (n) datatype |
|
Dynamic arrays
The syntax for declaring DYNAMIC arrays is as follows:
Dim arrayName() as datatype ReDim arrayName(4)
HERE,
| Code | Action |
| Dim arrayName () datatype |
|
| ReDim arrayName(4) |
|
Array Dimensions
An array can be one dimension, two dimensions or multidimensional.
We will create a simple application. This application populates an Excel sheet with data from an array variable. In this VBA Array example, we are going to do following things.
Let do this exercise step by step,
Step 1 – Create a new workbook
Step 2 – Add a command button
Note: This section assumes you are familiar with the process of creating an interface in excel. If you are not familiar, read the tutorial VBA Excel Form Control & ActiveX Control. It will show you how to create the interface
Your GUI should now be as follows
Step 3 – Save the file
Step 4 – Write the code
We will now write the code for our application
Private Sub cmdLoadBeverages_Click()
Dim Drinks(1 To 4) As String
Drinks(1) = "Pepsi"
Drinks(2) = "Coke"
Drinks(3) = "Fanta"
Drinks(4) = "Juice"
Sheet1.Cells(1, 1).Value = "My Favorite Beverages"
Sheet1.Cells(2, 1).Value = Drinks(1)
Sheet1.Cells(3, 1).Value = Drinks(2)
Sheet1.Cells(4, 1).Value = Drinks(3)
Sheet1.Cells(5, 1).Value = Drinks(4)
End Sub
HERE,
Code | Action |
Dim Drinks(1 To 4) As String |
|
Drinks(1) = "Pepsi" |
|
Sheet1.Cells(1, 1).Value = "My Favorite Beverages." |
|
Sheet1.Cells(2, 1).Value = Drinks(1) |
|
Select the developer tab and ensure that the Design mode button is "off." The indicator is, it will have a white background and not a coloured (greenish) background. (See image below)
Click on Load Beverages button
You will get the following results
Download Excel containing above code
Summary
What is a Subroutine in VBA? A Subroutine in VBA is a piece of code that performs a specific task...
VBA Comparison Operators These are operators that are used to compare values. Comparison operators...
$20.20 $9.99 for today 4.6 (119 ratings) Key Highlights of VBA Tutorial PDF 85+ pages eBook...
Everybody in this country should learn how to program a computer... because it teaches you how to...
VBA Logical Operators: AND, OR, NOT Excel VBA Logical Operators Let's say you want to process a...
Creating VBA Form/GUI controls in Excel GUI is the acronym for Graphical User Interface. The GUI...