Convert 2D Arary To 1D or List – Excel VBA Macro – Quickest Method

Convert Array of 2 Dimension to 1 Dimension

In Excel VBA , we use 2D arrays quite often. This is because, we convert Excel Worksheet Range to Array and do the data list or string operations on the list. In most cases it will be a 2D array.

While converting a 2D array to 1D, we can do it 2 ways. Either convert all the Rows in to a list or Columns into list. Both these can be achieved by using the function mentioned in this code.

Not only converting, this code will help you in understanding how to get only a row or column from a 2D array without using looping thru each element. It saves some time.

You can opt to use only one method rather than using both row/column slicing method

Slice 2D Array Elements Quickly

Here is an example for the two methods:

2D Array – Range(“A1:D3”) – Array Size (3,4)

2D ArrayCol1Col2Col3Col4
Row1abcd
Row21234
Row3ExcelMacroVBAArray

2D to 1D Array – By Converting each Row

abcd1234ExcelMacroVBAArray

2D to 1D Array – By Converting each Column

a1Excelb2Macroc3VBAd4Array

Note that from the 2nd value on, each of these 1D array differs.  In the first one, it reads the “a,b,c,d” the first row, then 2nd row and goes on. In second 1D Array, it the columns values “a,1,Excel”, then next column and so on.

It up to your needs to use on these methods and not both. Similar to a range, the same method can also be used to convert a table or named range into an Array.

The .resize function used in the code is mandatory to tell Excel what would be the exact size of the array that we are writing into worksheet. So, we initialize a range to address “A1”. Then, we resize it as per the size of the array using the comamnd UBound(Arr,1). This gives the number of items in the 1D Array.