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.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758Sub Convert_2D_To_1D_Array()Dim rng As Range, arr As Variant'Convert Rows to 1DSet rng = ThisWorkbook.Sheets("Sheet2").Range("A1")arr = Array_2D_To_1D("R")Set rng = rng.Resize(1, UBound(arr, 1) + 1)rng = arr'Convert Columns to 1DSet rng = ThisWorkbook.Sheets("Sheet2").Range("A2")arr = Array_2D_To_1D("C")Set rng = rng.Resize(1, UBound(arr, 1) + 1)rng = arrEnd SubFunction Array_2D_To_1D(cr As String) As Variant'Declare Array as variant & RangeDim rArray() As Variant, rRange As Range, iRow As Double, iCol As DoubleDim d1Arr As Variant, app As Object, sStr As String'Initialize Range to Reference a portion of WorksheetSet rRange = ThisWorkbook.Sheets("sheet1").Range("A1:D3")Set app = Application'Assign Range to ArrayrArray = rRange.ValuesStr = ""'Convert to 1D Array - By RowIf cr = "R" ThenFor iRow = 1 To UBound(rArray, 1)d1Arr = app.Index(rArray, iRow, 0)If sStr = "" ThensStr = Join(d1Arr, ",")ElsesStr = sStr & "," & Join(d1Arr, ",")End IfNext iRowEnd If'Convert to 1D Array - By ColumnIf cr = "C" ThenFor iCol = 1 To UBound(rArray, 2)d1Arr = app.Transpose((app.Index(rArray, 0, iCol)))If sStr = "" ThensStr = Join(d1Arr, ",")ElsesStr = sStr & "," & Join(d1Arr, ",")End IfNext iColEnd If'Display 2D array converted to 1DMsgBox sStrd1Arr = VBA.Split(sStr, ",")Array_2D_To_1D = d1ArrEnd Function
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 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.
Convert 2D Arary To 1D or List – Excel VBA Macro – Quickest Method