×

Excel VBA Array Function

VBA Array Function: The Array function in VBA generates an array containing the given set of values.

Syntax

Array (Arglist)

Parameter

Arglist (required) – This parameter the list of values that you want to make up the array wherein the values are separated by commas.

Return

This function returns an array containing the given set of values.

Example 1

Sub ArrayFunction_Function1()
 'declaring the array
 Dim names
 'inserting values in the array
 names = Array("Alan", "Christopher", "Elmer", "Frank")
 'running a loop to print the array
 For i = 0 To 3
     Cells(i + 2, 1).Value = names(i)
 Next
 End Sub 

Output

Name
Alan
Christopher
Elmer
Frank
VBA Array Function

Example 2

Sub ArrayFunction_Example2()
 'declaring the array variables
 Dim Index
 Dim names
 'inserting values in the array for column 1
 names = Array("Reema", "Varun", "Amar", "Shukla", "Pradeep", "Govind", "Shradha", "Sourav", "Kumar", "Twinkle")
 'inserting values in the array for column 2
 Index = Array("15023027", "16023014", "15023090", "15023012", "16023021", "16023087", "16023114", "16023123", "15023019", "15023456")
 'running a loop to print the Array
 For i = 0 To 9
     Cells(i + 2, 1).Value = names(i)
     Cells(i + 2, 2).Value = Index(i)
 NextR
 End Sub 

Output

Names Identity Number
Reema 15023027
Varun 16023014
Amar 15023090
Shukla 15023012
Pradeep 16023021
Govind 16023087
Shradha 16023114
Sourav 16023123
Kumar 15023019
Twinkle 15023456
VBA Array Function

Example 3

Sub ArrayFunction_Example3()
 'declaring the array
 Dim arry(5, 2)
 'running the loop to take data from user
 For i = 0 To 5
     IB = InputBox("Enter your First name")
     IB_sales = InputBox("Hey " + IB + "! Enter your sales amount")
     arry(i, 1) = IB
     arry(i, 2) = IB_sales
 Next
 'running a loop to print the array
 For i = 0 To 5
     For j = 1 To 2
         Cells(i + 2, j).Value = arry(i, j)
     Next
 Next
 End Sub 

Output        

VBA Array Function
VBA Array Function
VBA Array Function
VBA Array Function
VBA Array Function
VBA Array Function
Name Sales Amount
Reema 4567
Pradeep 9876
Rahul 12345
Rita 6789
Sunita 9876
Sheena 4356

Related Topics

Excel VBA IsEmpty Function

VBA IsEmpty Function: The IsEmpty function in VBA returns a Boolean value showing whether the specified Expression is Empty (variant has not been declared) or not. Syntax IsEmpty (Expression) Parameter Expression (required)- This parameter...

1 minute read.

Excel VBA IsNull Function

VBA IsNull Function: The IsNull function in VBA returns a Boolean, indicating whether a supplied expression is Null. Syntax IsNull (Expression) Parameter Expression (required)- This parameter represents the name of the argument that you want...

1 minute read.

Excel VBA Functions

What is a function? A function is also called a procedure, but it is not a sub procedure, it’s a function procedure. You have already been using some function in Excel,...

7 minutes read.

VBA ListBox

What is a ListBox? ListBox refers to a permanently displayed control (usually box-shaped) which contains a list of objects (or attribute, or elements) from which the user can select single or multiple attributes....

8 minutes read.

Excel VBA Weekday Function

Excel VBA Weekday Function: The TimeValue function in VBA returns an integer (1 to 7), signifying the day of the week for the specified date. Syntax Weekday (Date, [FirstDayOfWeek]) Parameter Date (required) – This parameter...

2 minutes read.

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.

Excel VBA Mid Function

VBA Mid Function: The Mid function in VBA returns a substring from within a supplied string. Syntax Mid (Str, Start, [Length]) Parameter Str (required) -This parameter represents a string from which you want to extract the substring. Start...

1 minute read.

Excel VBA UCase Function

The UCase function in VBA converts a String into upper case text. Syntax UCase (String) Parameter String (required) – This parameter represents the string that you want to convert to upper case. Return This function returns a string...

1 minute read.

Excel VBA CVErr Function

VBA CVErr Function: The CVErr function in VBA returns an Error data type, involving with a user-specified error code. Syntax CVErr (Expression) Parameter Expression (required)- This parameter represents the required error code. Return This function returns an...

1 minute read.

Excel VBA Timer Function

Excel VBA Timer Function: The Timer function in VBA returns a Single data type, evaluating the number of seconds that have elapsed since midnight of the current day. Syntax Timer () Parameter NA Return This...

1 minute read.

VBA Regex

What is a Regex? Regex stands for Regular Expression is basically a pattern matching strings within another string. They are supported in many languages, including .net, C++, Python, etc. They are...

5 minutes read.

VBA Regex Pattern Validation

VBA Regex Pattern Validation The key purpose of using Regex in VBA was to validate the data and extract the same category of data. There are certain formats that are standard...

6 minutes read.

Excel VBA: DO WHILE….Loop

DO WHILE….Loop The “Do While” Loop is the same, unlike the FOR statement, just that it will keep on looping till the specified condition is true. It is used when we want to...

3 minutes read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

VBA Find Function

VBA Find Function The Excel VBA FIND function finds any information in your Excel. It can be used on a Range object on the worksheet. It works the same, unlike the Excel Find &...

6 minutes read.

Excel VBA: IF THEN Statement

VBA Excel: IF THEN StatementThis conditional statement enables you to check one condition and on the basis of that then run one or multiple statements if the condition holds. If...

1 minute read.

Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute read.

Excel VBA WeekdayName Function

Excel VBA WeekdayName Function: The WeekdayName function in VBA returns a string containing the weekday name, for the specified integer representation of a weekday. Syntax WeekdayName (Weekday, [Abbreviate], [FirstDayOfWeek]) Parameter Weekday (required) – This parameter represents...

2 minutes read.

Excel VBA: IF…..THEN …ELSE Statement

VBA : IF…..THEN …ELSE Statement: This function enables you to check one condition and, based on that, then run one of the two statement blocks present. If the ‘IF’ condition...

2 minutes read.

Excel VBA IsMissing Function

VBA IsMissing Function: The IsMissing function in VBA checks if any parameter to a procedure is missing or not. It returns a Boolean value True if the specified parameter has not been...

1 minute read.