Trapping errors in mmult when used in VBA

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
I have made a little application for my daughter - her math class is learning 3 equations in 3 unknowns. (It's really more for me - when I'm checking her hw :wink: ). Here is the problem. It is straightforward to trap errors in the two variable case BEFORE anything gets to mmult. You just have to check the slopes and the y-intercepts and your finished. I don't know ALL the criteria for three variables (to determine if there are no solutions or infinite solutions) Does anyone on the board know either

1) All the criteria

2) or how to error trap mmult (Do you just put the equation inside an on error for example?)

Gene, "The Mortgage Man", Klein
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
hi gene.

this short code should handle the MMULT equation for you. as a tip, a solvable system of equations must have as many linearly independent equations as unknowns. by linearly independent, i mean that

1) you cannot multiply one equation by a constant and get another equation
2) you cannot add a constant to an equation and get another equation
3) some combination of (1) or (2)

cheers. ben.

ps. please note that this code outputs the multiplied matrix starting in Cell A1, so be careful not to overwrite any of your inputs!
Code:
Sub MultiplyMatrices()
    
    Dim rng1 As Range, rng2 As Range, c As Range
    Dim answer
    
    On Error GoTo EH:
    
    Set rng1 = Application.InputBox("Select the first matrix: ", Type:=8)
    Set rng2 = Application.InputBox("Select the second matrix: ", Type:=8)
        
    For Each c In Union(rng1, rng2).Cells
        If c.Value = "" Or Not IsNumeric(c.Value) Then
            MsgBox "Selection may not contain blanks or non-numeric entries."
            Exit Sub
        End If
    Next c
    
    If rng1.Rows.Count = rng2.Columns.Count Then
        answer = Application.WorksheetFunction.MMult(rng1, rng2)
        Range(Cells(1, 1), Cells(rng1.Rows.Count, rng2.Columns.Count)).Value = answer
    End If
   
EH:
'   Empty object variables
    Set rng1 = Nothing
    Set rng2 = Nothing
    Set c = Nothing
End Sub
 
Upvote 0
To solve AX=B where A, B, and X are matrices/vectors, the solution is X=B(A-inverse) given that A-inverse exists. So, the solution you want is MMULT(B,MINVERSE(A)) And, A-inverse exists if the determinant of A is non zero. In XL, the determinant is computed as MDETERM(A).

I have made a little application for my daughter - her math class is learning 3 equations in 3 unknowns. (It's really more for me - when I'm checking her hw :wink: ). Here is the problem. It is straightforward to trap errors in the two variable case BEFORE anything gets to mmult. You just have to check the slopes and the y-intercepts and your finished. I don't know ALL the criteria for three variables (to determine if there are no solutions or infinite solutions) Does anyone on the board know either

1) All the criteria

2) or how to error trap mmult (Do you just put the equation inside an on error for example?)

Gene, "The Mortgage Man", Klein
 
Upvote 0
To solve AX=B where A, B, and X are matrices/vectors, the solution is X=B(A-inverse) given that A-inverse exists. So, the solution you want is MMULT(B,MINVERSE(A)) And, A-inverse exists if the determinant of A is non zero. In XL, the determinant is computed as MDETERM(A).
[/quote]

I knew this information, but I didn't think to use it here :oops: . Thanks for pointing it out. I have never liked "on error". I guess I know I make so many mistakes, that I am always afraid that on error will not catch what I think it is catching! With yours, it is only catching the error that I want it to.

Gene, "The Mortgage Man", Klein
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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