Converting a Single Row Vector into a Matrix

October 18th, 2008 | Categories: VBA | Tags: , , , ,

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:

Function Create_Matrix(Vector_Range As Range, No_Of_Cols_in_output As Integer, No_of_Rows_in_output As Integer) As Variant
ReDim 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.

  1. January 13th, 2009 at 20:54
    Reply | Quote | #1

    Thank you for the article entitled Converting a Single Row Vector into a Matrix. I need to perform similar operations, but I don’t know how to use your function.

    Can you please, please, please provide the instructions explicitly showing how to actually use your function from within a VBA subroutine and generate the output displayed after “And we get:”?

    Thank you in advance.

    Jeff

  2. Samir
    February 6th, 2009 at 15:00
    Reply | Quote | #2

    One mistake: We need to call it with Create_Matrix and not Create_to_matrix.

    Even calling it correctly does not work. It always returns 0 in the cell I enter the formula. Could you please fix and re upload the macro. I need it urgently. Thanks

  3. Bastiaan
    June 21st, 2009 at 10:02
    Reply | Quote | #3

    The function works fine. Samir, yr right, just copy paste the function into VBA by:

    - Get into VBA (Press Alt+F11)
    Insert a new module (Insert > Module)

    - Copy and Paste the Excel user defined function examples

    - Get out of VBA (Press Alt+Q)
    Use the functions (They will appear in the Paste Function dialog box, Shift+F3, under the “User Defined” category)

    Well now, if you read the function, you see that it says temp_array at the end (your 0 return should have been the indicator!) as the formula needs to be entered as an array. You do so by selecting the area that is your matrix (starting from the top left), then click =, then click SHIFT+F3 to input the user defined function (Create_Matrix), and you hit CNTRL+SHIFT+ENTER to enter the array formula. Voila :)

    • Martha
      April 6th, 2010 at 22:35
      Reply | Quote | #4

      I have been at trying to use Create_Matrix nearly all day. As another user commented, it simply always returns a zero for me, even following the steps that Bastiaan (June 21st, 2009) suggests. Can anyone offer help?

  4. Rohini
    July 18th, 2009 at 13:35
    Reply | Quote | #5

    how can i use the downloaded files with excel? please tell me the steps.