×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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!

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?

(OP)
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

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

Just another, somewhat shorter solution:

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?

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources