To convert a range to an array in VBA, use the Value or Value2 property of the Range object: Dim arr As Variant: arr = Range("A1:C10").Value. This single line creates a 2-dimensional array (1-based, rows first, columns second) that mirrors the range’s data, and it is the fastest way to read or write large blocks of cells in Excel.
Why Convert a Range to an Array?
Working directly with Range objects in a loop is slow because every cell access crosses the Excel object model boundary. Reading a range into an array in one operation eliminates that overhead, making code dramatically faster. For example, looping through 100,000 cells one by one can take seconds, but reading them all into an array and looping in memory is nearly instant. Arrays also let you manipulate data without affecting the worksheet until you write back, which reduces screen flicker and accidental changes.
How the Conversion Works
When you assign a range to a Variant variable, VBA automatically creates a 2-dimensional array. The dimensions are always 1 To rowCount and 1 To colCount, regardless of the range’s location. For a single cell, the result is a 1-element, 2-dimensional array (not a scalar). For a single row or single column, it is still 2-dimensional, which catches many beginners off guard.
Example:
Dim data As Variant
data = Range("A1:D5").Value ' 5 rows x 4 columns
' data(1,1) is A1, data(5,4) is D5
To read a single column as a 1D array, use Application.Transpose:
Dim colData As Variant
colData = Application.Transpose(Range("A1:A10").Value)
' Now colData(1) to colData(10) are the values
Similarly, Application.Transpose can turn a 1D array into a column range when writing back.
Choosing Between Value, Value2, and Text
The Value property returns dates as VBA Date types and currency as Decimal, preserving formatting. Value2 returns the underlying raw value (dates as numbers, currency as Double) and is slightly faster because it skips date/currency conversion. Text returns what is displayed in the cell, including formatting like number formats, but it is read-only and slower. For most data processing, use Value2 to avoid type surprises; use Value when you need actual dates; use Text only when you need formatted strings.
Writing an Array Back to a Range
To write an array to a range, the target range must match the array’s dimensions. Assign the array to the range’s Value property:
Dim outArr(1 To 3, 1 To 2) As Variant
outArr(1,1) = "Name": outArr(1,2) = "Score"
outArr(2,1) = "Alice": outArr(2,2) = 95
outArr(3,1) = "Bob": outArr(3,2) = 88
Range("F1").Resize(3, 2).Value = outArr
If the array is 1-dimensional, you must transpose it before writing to a row or column. For a row, use Application.Transpose on a 1D array; for a column, you need a 2D array or transpose twice. A common trick: Range("A1").Resize(UBound(arr), 1).Value = Application.Transpose(arr) writes a 1D array down a column.
Common Problems and Misconceptions
- Array is 1-based, not 0-based: Unlike arrays declared with
Dim arr(0 To 9), the range-created array always starts at index 1. Accessingdata(0,0)throws a subscript error. - Single cell returns a 2D array:
Range("A1").Valuereturnsdata(1,1), not a scalar. UseRange("A1").Value2(1,1)orRange("A1").Valuedirectly if you only need one value. - Empty cells become Empty: If a cell is blank, the array element is
Empty, not""or0. Test withIsEmpty(). - Transpose limitation:
Application.Transposefails if the array has more than 65,536 elements (older Excel limit) or if the array is too large. UseApplication.Indexor a manual loop for very large arrays. - Range must be contiguous: You cannot directly convert a non-contiguous (multi-area) range to an array. Use
Unionareas individually or copy to a temporary sheet.
Performance Tips and Best Practices
- Read and write in bulk: avoid loops that touch cells. Read once, process in memory, write once.
- Use
Value2overValuewhen you don’t need date/currency conversion. - Declare the array as
Variant; you cannot declare it as a specific type (e.g.,String) because the range conversion returns a Variant array. - When processing a large range, consider limiting to the used range:
Range("A1").CurrentRegion.Value. - If you need to modify only a few cells, don’t convert the whole range; just edit those cells.
- Always save a copy of your workbook before running a macro that writes to a range, because array assignment overwrites cell contents and cannot be undone with Ctrl+Z.
Alternatives to Range-to-Array
If you need to process data without VBA, consider using Excel formulas like FILTER, SORT, or UNIQUE (available in Excel 2021 and Microsoft 365) which return dynamic arrays. For simple tasks, Power Query (Get & Transform) can load and transform ranges without code. In VBA, you can also use ArrayList (from .NET) or Collection for dynamic lists, but they are slower for bulk numeric operations. The Range.Value method remains the fastest and simplest for reading/writing rectangular blocks.
Frequently Asked Questions
How do I convert a range to a 1D array?
Use Application.Transpose on the 2D array. For a single row, Dim arr As Variant: arr = Application.Transpose(Range("A1:C1").Value) gives a 1D array with 3 elements. For a single column, the same works: Application.Transpose(Range("A1:A10").Value) gives a 1D array with 10 elements. Note that Transpose has a size limit (around 65,536 elements in older versions); for larger ranges, loop manually.
Why does my array start at 1 instead of 0?
Because the range’s Value property returns a 2D array that is always 1-based in both dimensions. This is by design in VBA. If you need 0-based indexing, you must copy the values into a manually declared array using a loop, or use Application.Index to slice.
Can I convert a range to an array without using Variant?
No. The Value property returns a Variant containing an array. You cannot assign it directly to a typed array (e.g., Dim arr() As String) without a conversion loop. If you need a typed array, declare a dynamic array and fill it element by element from the range.
How do I convert an array back to a range with headers?
Include the headers in the array itself. For example, if your array has 2 columns and 101 rows (row 1 headers), write it to Range("A1").Resize(101, 2).Value = arr. The headers are just the first row’s values.
What is the fastest way to read a large range into an array?
Use Range.Value2 on the entire contiguous range in one line. For example, Dim data As Variant: data = Range("A1:D100000").Value2 reads 400,000 cells in a fraction of a second. Avoid splitting the range into smaller pieces unless memory is a concern.
Does converting a range to an array copy formulas or values?
It copies the resulting values, not the formulas. If a cell contains a formula, the array gets the calculated result. To get the formula strings, use the Formula property on the range, but that returns a 2D array of strings, not values.
Related
- Splitting Strings in Java: Methods and Examples
- Generate Random Number in Oracle SQL?
- Button Cell Battery Components: What’s Inside
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Insert Row Below Active Cell: Code and TipsTo insert a complete worksheet row immediately below the active cell in Excel for Microsoft 365 on Windows,…
- Excel VBA Editor: How to Open and Use ItThe Excel VBA Editor, also called the Visual Basic Editor or VBE, is the desktop window where you…
- Selection in VBA: How to Select Cells, Ranges & MoreIn VBA, “Selection” refers to the currently selected object in the active workbook—most commonly a cell, a range…
- VBA Error Handling Tutorial: Errors, Handlers, and CleanupVBA error handling lets a macro respond to a runtime problem instead of stopping unexpectedly. Start with On…
- Excel VBA Find Text in a String: InStr ExplainedIn Excel VBA, use the InStr function to find one piece of text inside another: it returns the…
- Excel VBA Function Return: Values, Syntax, and ExamplesAn Excel VBA function returns a value by assigning that value to the function’s own name; VBA does…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.