• ## 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

Also Read: Excel VBA - Merge Cells - Unmerge Range

## Slice 2D Array Elements Quickly

Here is an example for the two methods:

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

 2D Array Col1 Col2 Col3 Col4 Row1 a b c d Row2 1 2 3 4 Row3 Excel Macro VBA Array

2D to 1D Array – By Converting each Row

 a b c d 1 2 3 4 Excel Macro VBA Array

2D to 1D Array – By Converting each Column

 a 1 Excel b 2 Macro c 3 VBA d 4 Array

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.

Author: Kumarapush

Kumarapush has experience in IT industry for 12+ Years including 10 Years in MNC. Has vast expertise in Microsoft Excel automation using VBA Macro. Excel to CSV converter is one of his famous tool that has been downloaded & used by 68,000 users around the globe.