Matrix Worksheet Function MInverse in VBA with Array

trough

Board Regular
Joined
Oct 26, 2010
Messages
55
I have a VBA script error out with "Type mismatch" on the Application.WorksheetFunction MInverse line. How else am I suppose to calculate a matrix inverse? What is the type mismatch? Both arrays are the same size and the matrix defined is invertible.

Option Explicit
Option Base 1

Function CalcB()

Dim A() As Double
Dim B() As Double
ReDim A(1 To 2, 1 To 2)
ReDim B(1 To 2, 1 To 2)

A = CalcA()

B = Application.WorksheetFunction.MInverse(A)

Range("A1:B2") = B

End Function


Private Function CalcA()

Dim aAr() As Double

ReDim aAr(1 To 2, 1 To 2)

aAr(1, 1) = 2.8
aAr(2, 2) = 7.3
aAr(1, 2) = 5.7
aAr(2, 1) = 1.7

CalcA = aAr

End Function
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Declare A and B as varitant

Try this:

VBA Code:
Option Explicit

Option Base 1

Function CalcB()
  Dim A As Variant
  Dim B As Variant
  ReDim A(1 To 2, 1 To 2)
  ReDim B(1 To 2, 1 To 2)
  A = CalcA()
  B = Application.WorksheetFunction.MInverse(A)
  Range("A1:B2") = B
End Function

Private Function CalcA()
  Dim aAr() As Double
  ReDim aAr(1 To 2, 1 To 2)
  aAr(1, 1) = 2.8   'row, column
  aAr(1, 2) = 5.7   'row, column
  aAr(2, 1) = 1.7   'row, column
  aAr(2, 2) = 7.3   'row, column
  CalcA = aAr
End Function
 
Upvote 0
Solution
Had to declare aAr in CalcA as Variant as well and then it worked. I'm not very versed on the Variant data type but I think I need to get that way now. Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
Members
453,021
Latest member
Justyna P

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