Excel VBA Array

VBA One-dimensional Array  example |  VBA two-dimensional Array  example

This post will guide you how to create one-dimensional array and two-dimensional array in MS excel VBA.

Array is a specific variable and it can hold more than one value in a single variable.

VBA One-dimensional Array Declaration

Array declaration can be used the following three syntax:

  1. Dim array1()
  2. Dim array2(1 to 5)
  3. array3 = Array(“one”, “two”, “three”)

VBA One-dimensional Array assignation

This section will describe that how to assign value to VBA array.

Dim array2(1 To 5) As String
array2(1) = "excel"
array2(2) = "word"
array2(3) = "access"
array2(4) = "ppt"
array2(5) = "oneNote"

Example1:  VBA One-dimensional Array  example

1# open visual Basic Editor, then insert a module and name as :myOneDimensionalDemo1

2# enter into the below VBA codes in code window.

Sub myOneDimensionalDemo1()
Dim array2(1 To 5) As String
array2(1) = "excel"
array2(2) = "word"
array2(3) = "access"
array2(4) = "ppt"
array2(5) = "oneNote"
MsgBox "the value of array2(1) is " & array2(1)
End Sub

excel array example1

3# back to workbook and run the above macro.

excel array example2

VBA One-dimensional Array example1

VBA two-dimensional Array Declaration

If you want to declare a string two-dimensional array, just refer to the below lines:

Dim array1(2,2) as String

x
How to Highlight Every Other Row Using Conditional Formatting in Excel

Or

Dim array1(1 to 2, 1 to 2) as String

This Array declaration will declare with 2 rows and 2 columns.

VBA Two-dimensional Array assignation

This section will describe that how to assign value to VBA two-dimensional array.

Dim array1(1 to 2, 1 to 2) as String
Array1(1,1) = "excel"
Array1(1,2) = "word"
Array1(2,1) = "access"
Array1(2,2) = "ppt"

Example2:  VBA two-dimensional Array  example

1# open visual Basic Editor, then insert a module and name as :myTwoDimensionalDemo1

2# enter into the below VBA codes in code window.

Sub myTwoDimensionalDemo1()
Dim array1(1 To 2, 1 To 2) As String
array1(1, 1) = "excel"
array1(1, 2) = "word"
array1(2, 1) = "access"
array1(2, 2) = "ppt"
MsgBox "the value of array1(1,1) is " & array1(1, 1)
End Sub

excel array example3

3# back to workbook and run the above macro.

excel array example4

VBA two-dimensional Array example1

 

Related Posts

How To Remove Special Characters in Excel
remove special character2

This post will show you how to remove special characters from text strings in Excel. And I am going to introduce two ways to remove special characters (@&)#%$) from a string in Excel. Removing Special Characters using Power Query If ...

How To Insert Comments in Protected Worksheet in Excel
insert commetns in protected workshee3

This post will show you how to allow comments in a protected worksheet in Excel. You can easily to insert comments into cells in a normal worksheet in Excel, but if want to insert a comment in a worksheet that ...

How To Convert Text to Upper Cases(Using VBA) in Excel
convert text to upper cases1

This post will show you how to switch from lower case to upper case in Excel. and I am going to show you two different ways of converting text to upper cases using formula or VBA macro in Excel 2013,Excel ...

How To Hide Every Other Row in Excel (Using VBA)
hide every other row1

This post will show you how to hide alternate rows or columns in Excel or how to hide every third, fourth, fifth row or column in Excel. If you want to hide every other row in your current worksheet, how ...

How to Disable the Save As Prompt in Excel
disable save as prompt1

This post will show you how to use a VBA Macro to save an Excel file and overwrite any existing file without a prompt so that you are going to get the little window that says file already exists do ...

How to Count Cells that Contain even or odd numbers in Excel
count cells that contain even numbers5

This post will guide you how to count the number of cells that contain odd or even numbers within a range of cells using a formula in Excel 2013/2016.How do I count cells that contain odd numbers through the use ...

How to Count Cells Are Not Blank or Empty in Excel
count non blank nonempty cells5

This post will guide you how to count cells that are not blank or empty in a given range cells using a formula in Excel 2013/2016.How do I count the number of cells that are not blank in a particular ...

How to Count Cells Less Than a Specific Value in Excel
count cells lessr than5

This post will guide you how to count the number of cells less than a particular numeric value in a given range cells using a formula in Excel 2013/2016. How do I count cells that are less than a specific ...

How to Count Cells Greater Than a Specific Value in Excel
count cells greater than 5

This post will guide you how to count the number of cells greater than a particular numeric value in a given range cells using a formula in Excel 2013/2016. How do I count cells that are greater than a specific ...

How to Remove All Extra Spaces and Keep Only One Between Words in Excel
How to Remove All Extra Spaces and Keep Only One Between Words 7

Sometimes when copying something from other type files to excel, there might be two or more spaces display between words. Extra spaces between words are frequently to be seen in excel, and this behavior is very annoying. If we want ...

Sidebar