How to Use Arrays Instead of Ranges in Excel VBA

An object that is a collection of random elements is called an array. When an item knows how to move from one end to the other without having to list every element in between, that object is said to have a range. A loop across an array moves much more swiftly than a range search, which makes it 96.39% faster to loop through an array than through a loop. The discrepancy will be greater the more values you have. In this article, we will show you how to use arrays instead of ranges in Excel VBA.


Download Practice Workbook

You may download the following Excel workbook for better understanding and practice yourself.


Step-by-Step Procedures to Use Arrays Instead of Ranges in Excel VBA

Multiple records of identical names and similar types are stored in arrays. It enables access to elements at random. There is neither a memory deficit nor an overflow because the array is fixed in size and stored in adjacent memory locations. Any type of data with a fixed size should be stored. Here, we will generate a VBA code using an array instead of ranges in Excel VBA. Let’s suppose we have a sample data set.

Step-by-Step Procedures on How to Use Arrays Instead of Ranges in Excel VBA


Step 1: Utilizing Developer Tab to Start a VBA Macro

The Excel built-in Developer tab offers the tools required to use Visual Basic for Applications (VBA) to execute a macro operation. By default, the tab is disabled. To make it visible on the toolbar at the top of the Excel window, it must first be activated in the Options section of the File menu.

  • Firstly, select the Developer tab.
  • Secondly, click on the Visual Basic command.

Utilizing Developer Tab to Start a VBA Macro

Read More: How to Create an Array in Excel VBA (4 Ideal Methods)


Step 2: Creating New Module to Generate VBA Code

VBA is a programming language that may be used for a variety of tasks, and different types of users can use it for those tasks. You can launch the VBA editor using the Alt + F11 keyboard shortcut.

  • Now, the Visual Basic Application window will open.
  • Next, select the Insert tab.
  • Then, click on the Module option to create a new Module to write a VBA code.

Creating New Module to Generate VBA Code

Read More: How to Convert Range to Array in Excel VBA (3 Ways)


Similar Readings


Step 3: Applying VBA Code

Applying VBA code is a very convenient method to do any Excel calculations. In this section, we will generate a VBA code using an array instead of ranges.

  • Now, paste the following VBA code into the Module
  • Finally, to run the program, click the “Run” button or press F5.
Sub Using_Arrays()
'Declaring variable
Dim arr As Variant
Dim i As Long
'Calling our activesheet
arr = ActiveSheet.Range("B4").CurrentRegion
'Returns the following values for an array
For i = LBound(arr, 1) To UBound(arr, 1)
Next i
'Showing the values in the activesheet
ActiveSheet.Range("H4:K10").Value = arr
End Sub

 Applying VBA Code to Show How to Use Arrays Instead of Ranges in Excel VBA

VBA Code Breakdown

  • Firstly, we specify a name for the subject as Sub Using_Arrays().
  • Secondly, we declare a variable as Dim arr As Variant and Dim i As Long.
  • Thirdly, we call our worksheet as arr = ActiveSheet.Range(“B4”).CurrentRegion.
  • Then, we return the values from the worksheet for an array as For i = LBound(arr, 1) To UBound(arr, 1).
  • Finally, we apply a statement to show the values in the active sheet as Range(“H4:K10”).Value = arr.

Read More: How to Name a Table Array in Excel (With Easy Steps)


Step 4: Demonstrating Final Results

  • In the last section here, we will show the results where you will see the actual data set on the left side named “applying VBA Code” as well as the output data set on the right side.

Demonstrating Final Results

Read More: VBA to Get Unique Values from Column into Array in Excel (3 Criteria)


Conclusion

In this article, I’ve covered step by step process of how to use arrays instead of ranges in Excel VBA. I sincerely hope you enjoyed and learned a lot from this article. Additionally, if you want to read more articles on Excel, you may visit our website, Exceldemy. If you have any questions, comments, or recommendations, kindly leave them in the comment section below.


Related Articles

Bishawajit Chakraborty

Bishawajit Chakraborty

I'm Bishawajit Chakraborty. Hello. I graduated from Rajshahi University of Engineering & Technology (RUET) with a degree in Mechanical Engineering. I'm working with ExcelDemy as a Content Developer for Excel & VBA. You can visit our website, Exceldemy if you'd like to read my published articles on MS Excel and VBA.

We will be happy to hear your thoughts

Leave a reply

5 Excel Tips
You Never Knew

Genius tips to help you unlock Excel's hidden features

FREE EMAIL BONUS

ExcelDemy
Logo