### Converting a Single Row Vector into a Matrix

##### Associated Files Download Links

## Convert Single Row Vector into a Matrix

This is the opposite tutorial of the Convert Matrix to Vector tutorial.

The following function takes a single row vector and converts into a matrix:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Function Create_Matrix(Vector_Range As Range, No_Of_Cols_in_output As Integer, No_of_Rows_in_output As Integer) As Variant <span style="border: 0pt none;margin: 0pt;padding: 0pt;background: transparent none repeat scroll 0% 0%;font-style: normal;font-variant: normal;font-weight: bold;font-size: 100%;line-height: normal;text-align: left;text-indent: 0pt;cursor: pointer;font-family: serif;color: red;text-decoration: underline">ReDim</span> Temp_Array(No_Of_Cols_in_output, No_of_Rows_in_output) Dim No_Of_Elements_In_Vector As Integer Dim Col_Count As Integer, Row_Count As Integer Dim Cell No_Of_Elements_In_Vector = Vector_Range.Rows.Count 'Eliminate NULL Conditions If Vector_Range Is Nothing Then Exit Function If No_Of_Cols_in_output = 0 Then Exit Function If No_of_Rows_in_output = 0 Then Exit Function If No_Of_Elements_In_Vector = 0 Then Exit Function For Col_Count = 1 To No_Of_Cols_in_output For Row_Count = 1 To No_of_Rows_in_output Temp_Array(Col_Count, Row_Count) = Vector_Range.Cells(((No_of_Rows_in_output) * (Col_Count - 1) + Row_Count), 1) Next Row_Count Next Col_Count Create_Matrix = Temp_Array End Function |

The function takes 3 arguments:

• The range of the initial vector

• The number of rows required in the matrix

• The number of columns required in the matrix

So if we have:

Then we can call our function:

Convert_to_matrix(c7:c16,2,5)

Where:

1. c7:c16 is the matrix range

2. 2 is the number of rows required in the matrix

3. 5 is the number of columns required

And we get:

**To download the .XLSM file from this function, click here.**

## VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

**Did you find this VBA tutorial useful? Then share it with your friends and colleagues:**