How can I solve a matrix from VB with Excel commands?
How can I solve a matrix from VB with Excel commands?
(OP)
I would like to know how could I get a solution to solve a matrix from VB using a Excel command.
For exemple, I try to solve a determinant:
1 2
3 4 = -2
Dim obj As Excel.Application
Set obj = CreateObject("Excel.Application")
Solution = obj.Application.MDeterm(1,2;3,4)
But VB doesn't allow me to write the ";"
It is very similar if I try to solve a Inverse Matrix:
Solution = obj.Application.MInverse(1,2;3,4)
Or if I try with a variable:
A(1,1)=1 : A(1,2)=2 : A(2,1)=3 : A(2,2)=4
Solution = obj.Application.MDeterm(A())
it gaves to me Solution = 0
Thanks a lot & sorry for my english!
For exemple, I try to solve a determinant:
1 2
3 4 = -2
Dim obj As Excel.Application
Set obj = CreateObject("Excel.Application")
Solution = obj.Application.MDeterm(1,2;3,4)
But VB doesn't allow me to write the ";"
It is very similar if I try to solve a Inverse Matrix:
Solution = obj.Application.MInverse(1,2;3,4)
Or if I try with a variable:
A(1,1)=1 : A(1,2)=2 : A(2,1)=3 : A(2,2)=4
Solution = obj.Application.MDeterm(A())
it gaves to me Solution = 0
Thanks a lot & sorry for my english!





RE: How can I solve a matrix from VB with Excel commands?
' try this:
Sub test()
Dim solution As Single
Dim myapp As Excel.Application
Dim mybook As Excel.Workbook
Dim mysheet As Excel.Worksheet
Dim mycells As Range
Set myapp = CreateObject("excel.application")
Set mybook = myapp.Workbooks.Add
Set mysheet = mybook.Worksheets.Add
mysheet.Cells(1, 1).Value = 1
mysheet.Cells(1, 2).Value = 2
mysheet.Cells(2, 1).Value = 3
mysheet.Cells(2, 2).Value = 4
Set mycells = mysheet.Range("A1:B2")
solution = myapp.MDeterm(mycells)
'I get solution = -2
End Sub
RE: How can I solve a matrix from VB with Excel commands?
It works OK !!!
I have used the same method to get the solution of an inverse matix (with an other sheet and calling cell by cell)
Thanks again!
Alan D.J. Atkinson
atkinson@unex.es
RE: How can I solve a matrix from VB with Excel commands?
Sub test1()
Dim mymatrix(1 To 2, 1 To 2) As Double, solution As Double
mymatrix(1, 1) = 1
mymatrix(1, 2) = 2
mymatrix(2, 1) = 3
mymatrix(2, 2) = 4
solution = Application.WorksheetFunction.MDeterm(mymatrix)
End Sub
RE: How can I solve a matrix from VB with Excel commands?
Also your solution goes OK.
The problem is how to get the solution of an other matrix (MInverse). I think that I should capture it with a sheet ¿isn’t it?
Have anybody used Mathematica libraries with VB?
Alan D.J. Atkinson
atkinson@unex.es