question still about using array variables in formula

novice_2010

Board Regular
Joined
Mar 18, 2010
Messages
105
Hello, All,

I have following codes:

Sub test()
Dim Array_1 As Variant
Dim Array_2 As Variant


Array_1 = Array(2, 4, 6, 8, 10)
Array_2 = Application.WorksheetFunction.Transpose(Array_1)


Range("A1").FormulaArray = "=mmult(" & Array_1 & "," & Array_2 & ")"

End Sub


As usual, I got a "Type mismatch" error message. How should I re-write the red line of code to make it work?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
If you want the formula in the cell:

Code:
Range("A1").FormulaArray = "=MMULT({1,2,3,4,5,6},{1;2;3;4;5;6})"

If you want the reslut:

Code:
Sub T()
    Dim Array_1 As Variant
    Dim Array_2 As Variant
    Array_1 = Array(2, 4, 6, 8, 10)
    Array_2 = Application.WorksheetFunction.Transpose(Array_1)
    Range("A1").Value = WorksheetFunction.MMult(Array_1, Array_2)
End Sub
 
Upvote 0
Hi

If you want A1 to have a formula, not the result of the formula, like:

=MMULT({2,4,6,8,10},TRANSPOSE({2,4,6,8,10}))

Try:

Code:
Sub test()
Dim Array_1 As Variant
Dim sArray_1 As String
 
Array_1 = Array(2, 4, 6, 8, 10)
sArray_1 = "{" & Join(Array_1, ",") & "}"
 
Range("A1").FormulaArray = "=mmult(" & sArray_1 & ",transpose(" & sArray_1 & "))"
End Sub
 
Upvote 0
One thing I think is wrong is the dimensions of the arrays.
 
Upvote 0
Thank you.

So there is NO WAY just to put Array Variable Names (which are Array_1, Array_2 in my case) in the formula?

If that is true, it really surprises me, because the following code will work:


Sub test()
Dim Not_Array_1 As Long
Dim Not_Array_2 As Long


Not_Array_1 = 1
Not_Array_2 = 1


Range("A1").Formula = "=sum(" & Not_Array_1 & "," & Not_Array_2 & ")"

End Sub
 
Upvote 0
pgc

I know you can.

That wasn't really what I was thinking of.
 
Upvote 0
thank you all for suggesting.
I think pgc 's suggestion is good for 1-dimensional array; however it won't work for 2-dimensional array. As shown in the following codes:

Option Base 1
Sub test()
Dim Array_1 As Variant, Array_2 As Variant, Matrix As Variant
Dim sMatrix As String

Array_1 = Array(1, 2, 3)
Array_2 = Application.WorksheetFunction.Transpose(Array_1)

Matrix = Application.WorksheetFunction.MMult(Array_2, Array_1)
'Matrix is a 2 dimensional array variable


sMatrix = "{" & Join(Matrix, ",") & "}"

Range("A1").Resize(3, 3).FormulaArray = "=mmult(" & sMatrix & ",transpose(" & sMatrix & "))"
End Sub

How to make the formula in this example work?
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top