Skip to main content

New Article

How to SUM by matching partial text in Excel

An Introduction to Array in Excel VBA

Excel VBA is a Programming language, and though it is a language, it should support Array Programming. Yes, it does. In Excel we can use Array Formula. We can also use the power of Array in Excel VBA.



What is an Array?

In a simple definition, the variable, which contains two or more values under the same variable, is called an Array. In programming language sometime you need to use lookup function in a specific list. To do this use an Array. An Array is a way to store more than one value under the same variable name. An array looks like this when it is declared:


Dim yourName(5) as Integer

In the above code, Dim is short for the word Dimension and it allows you to declare variable names and their type. Then we used a custom name yourName to store values. But how many values can store in yourName? We declared (5) that means 0 to 5 = 6 values. If we declared (1 to 5) then it will not count the 0 (zero), so that you can store 5 values under the same yourName variable. So you can also set an Array variable with 6 stored cell like below:


Dim yourName(1 to 6) as Integer




How to set values in Array?

Now to set your Array Values under the same Array Variable, follow the below code:


Dim yourName(1 to 6) as Integer

yourName(1) = 4
yourName(2) = 2
YourName(3) = 7
YourName(4) = 3
YourName(5) = 8
YourName(6) = 7

In the above code, you entered 4, 2, 7, 3, 8, 7 digits as values for each variable cell (1 to 6) under the same variable name yourName.



Run the Array values:

To test your Array values working properly or not, use the below codes in your code window:


Sub ArrTest()

Dim yourName(1 to 6) as Integer

yourName(1) = 4
yourName(2) = 2
YourName(3) = 7
YourName(4) = 3
YourName(5) = 8
YourName(6) = 7

MsgBox yourName(4)

End Sub

If you run the above program, then it will show a message box. The message box shows the value which is stored in yourName(4). That means 3:


Image 1: Result

This is the basic of Array in Excel VBA. In next I will try to explain more details with the help of real life project.


|||| Please SUBSCRIBE our YouTube Channel ||||
https://www.youtube.com/channel/UCIWaA5KCwZzBGwtmGIOFjQw

Popular posts from this blog

How to display an image in worksheet based on a List or based on IF condition?

Excel can show image on worksheet based on a specific IF condition. So, how to do it? Simple follow the below steps: Step by Step: Step 1: Insert images in your Excel Worksheet. Here I've inserted 5 different types of balls, Football, Cricket, Pool, Basketball, Tennis ball. Note that, All balls are placed into different cell. These are placed in Picture sheet. Image 1: 5 balls placed in 5 different cells and covered photo's wide and height Step 2: In the report sheet, design the report as you wish. I've designed in my way like below: Image 2: Kids asking to Donald Duck, which ball need to throw now Step 3: Make a drop down list "Games" in E6 cell in Report sheet from Data Validation. which is as below: Football Cricket Pool Basketball Tennis You can do an IF function here in E6 in Report sheet, which will meet a certain condition and returned Football, Cricket, Pool, Basketball or Tennis. Step 4: Now the tricky part is...

Cumulative Closing Balance like a Bank Statement in PivotTable

A Bank Employee in many case needs to calculate the Closing Balance after each transaction in PivotTable like a Bank Statement . But with the help of Calculated Field of a PivotTable , you can only calculate Field with Field . It's not possible to use a formula in Calculated Field where you can mention a single cell with Relative Reference Cell . Because a PivotTable acts like a Table format where you can work with Field or Column Name . A Helping Column (A regular column, that helps to get a partial result where formula applied) can be a good idea. But there is an option in PivotTable by which you can Calculate Cumulative . In this article I will show you, how you can do it. Calculating with Formula: Assume that, you have an Excel file with Debit and Credit transaction of an Account . Image 1: Sample Bank Statement In a Bank Statement , Month wise Cumulative Balance is important. In the Excel sheet, as above, it is very easy to use a formula and calc...

Value Paste and Formula Paste

In many case, in our professional life, we need to do Copy and Value Paste or Copy and Formula Paste in all most always. Those employees, who works under MIS Department, have to do it lots of time in a single day to prepare reports. It is very time costing task in Office. Many of us use Excel Menubar to do it, others are using Keyboard Shortcut by pressing ALT key (like for Value Paste Press ALT+H+V+V and for Formula Paste Press ALT+H+V+F ). But did you noticed that, these two ways strongly need your attention to do this task. More clearly, if you choose Value Paste from Menubar , then what you need to do? First you should take the mouse in Hand Wheel the mouse in Home menu Then Click on Paste Then you pressure your eyes to find the Value Paste icon And then Click on it to paste it These all steps can take more than 1 second. Am I right? Below is an image of this process: Image 1: Value Paste from Home Menu On the other hand, if you choose the shortcut f...