Table of Contents

Array Function

The Array function in Excel VBA can be used to quickly and easily initialize an array. Place a command button on your worksheet and add the following code lines:

1. First create a variable named departments of type Variant.

Dim departments As Variant

2. Use the Array Function to assign an array to the variable departments. Add the following code line:

departments = Array("Sales", "Production", "Logistics")

3. To show the element with index 1, add the following code line:

MsgBox departments(1)

Result:

Array Function Result with Option Base 0

By default, the element’s index of the array starts from 0.

4. Add Option Base 1 to the General Declarations section if you want the index to start from 1.

Add Option Base 1 in Excel VBA

Result when you click the command button again:

Array Function Result with Option Base 1

Leave A Comment

Excel meets AI – Boost your productivity like never before!

At Formulas HQ, we’ve harnessed the brilliance of AI to turbocharge your Spreadsheet mastery. Say goodbye to the days of grappling with complex formulas, VBA code, and scripts. We’re here to make your work smarter, not harder.

Related Articles

The Latest on Formulas HQ Blog