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?
 
You can't transpose a string.

Code:
Sub test()
    Dim sMat1       As String
    Dim sMat2       As String
 
    sMat1 = "{" & Join(Array(1, 2, 3), ",") & "}"
    sMat2 = "{" & Join(Array(4, 5, 6), ",") & "}"
 
    Range("A1").FormulaArray = "=mmult(" & sMat1 & ",transpose(" & sMat2 & "))"
End Sub

... or

Code:
Sub test()
    Dim sMat1       As String
    Dim sMat2       As String
 
    sMat1 = "{" & Join(Array(1, 2, 3), ",") & "}"
    sMat2 = "{" & Join(Array(4, 5, 6), ";") & "}"
 
    Range("A1").FormulaArray = "=mmult(" & sMat1 & "," & sMat2 & ")"
End Sub
Also, this formula returns a single result, so it need not be an array formula.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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:

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

How to make the formula in this example work?

Hi again

You are right, that line converts a 1d array into the corresponding string.

If you have a 2d array, you must use other code to get the string.

For ex., a simple way is to do it in a Function, which leaves the main code simple.

Just replace that line of code with:

Code:
Option Explicit
Option Base 1

Sub test()
Dim Array_1 As Variant, Array_2 As Variant, vMatrix As Variant
Dim sMatrix As String
 
Array_1 = Array(1, 2, 3)
Array_2 = Application.WorksheetFunction.Transpose(Array_1)
vMatrix = Application.WorksheetFunction.MMult(Array_2, Array_1)

'Matrix is a 2 dimensional array variable
[COLOR=red][B][I]sMatrix = Conv2DArray(vMatrix)[/I][/B][/COLOR]
 
Range("A1").Resize(3, 3).FormulaArray = "=mmult(" & sMatrix & ",transpose(" & sMatrix & "))"
End Sub

The only difference with the previous code is the conversion of the 2d array to string.

To convert the array to string, use, for ex.:

Code:
Function Conv2DArray(vArray As Variant) As String
Dim j As Long
Dim s As String
 
For j = LBound(vArray, 1) To UBound(vArray, 1)
    s = s & ";" & Join(Application.Index(vArray, j, 0), ",")
Next j
Conv2DArray = "{" & Mid(s, 2) & "}"
End Function

Remark: I did not care about error handling.
 
Upvote 0
I'm glad it helped.

Remark:

I assumed this is a learning experience and you just want to know how to convert vba arrays to worksheet format.

If this was a real solution, what would make sense to me would be to do the calculations in vba and just write the result to the worksheet, like:

Code:
Sub test()
Dim Array_1 As Variant, vMatrix As Variant
 
Array_1 = Array(1, 2, 3)
 
With Application.WorksheetFunction
    vMatrix = .MMult(.Transpose(Array_1), Array_1)
    Range("A1").Resize(3, 3).Value = .MMult(vMatrix, .Transpose(vMatrix))
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,823
Members
452,946
Latest member
JoseDavid

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