VBA Programming Series...27 - IT Skills

This blog is for IT lovers, accounts and finance executives who want to add value in their professional life.

test

Welcome to our blog. This blog is for IT lovers, Accounts/Finance Executives who wants to add value in their career path.

Search This Blog

Wednesday, 6 June 2018

VBA Programming Series...27

test

Working with Arrays


Most programming languages support arrays. An array is a group of variables that share a common name. You refer to a specific variable in the array by using the array name and an index number in parentheses. For example, you can define an array of 12 string variables to hold the names of the months of the year. If you name the array MonthNames, you can refer to the first ele-ment of the array as MonthNames (1), the second element as MonthNames (2), and so on.


Declaring arrays


Before you can use an array, you must declare it. No exceptions. Unlike normal variables, VBA is very strict about this rule. You declare an array with a Dim or Public statement, just as you declare a regular variable. However, you also need to specify the number of elements in the array. You do this by specifying the first index number, the keyword To, and the last index number — all inside parenthe-ses. The following example shows how to declare an array of 100 integers:


Dim MyArray(1 To 100) As Integer

When you declare an array, you can choose to specify only the upper index. VBA assumes that 0 is the lower index. Therefore, both of the following state-ments declare the same 101-element array:


Dim MyArray (0 To 100) As Integer


Dim MyArray (100) As Integer


If you want VBA to assume that 1 (rather than 0) is the lower index for your arrays, include the following statement in the Declarations section of your module:


Option Base 1


This statement forces VBA to use 1 as the first index number for arrays that declare only the upper index. If this statement is present, the following state-ments are identical, both declaring a 100-element array:


Dim MyArray (1 To 100) As Integer



Dim MyArray (100) As Integer

No comments:

Post a Comment

Popular

Welcome to our blog. If you want to; learn writing skills, preparation for CSS/Corporate laws, IT skills, downloading Business/IT books, and many more; this blog is for you.