Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I solve a matrix from VB with Excel commands?

Status
Not open for further replies.

ADJ

Civil/Environmental
Oct 17, 2001
3
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!
 
Replies continue below

Recommended for you


' 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

 
Thanks a lot!!!!
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
 
Just another, somewhat shorter solution:
Code:
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
 
Thanks to YAKPOL

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor