Hi. I need to write a macro on VBA that can solve the equations Ax + By = C and Dx + Ey = F. The macro should be run using a button, and the output will be a message box that displays the x and y values. A through F should go in the first column of the spreadsheet, and they are not zero. I can't find very much information online about how to do this. However, I found the answer to this question on another forum, but can someone write comments to break it down? Also, when I ran this, there was an error with the line
"X = WorksheetFunction.MMult(WorksheetFunction.MInverse(A), B)". Thanks.
Option Explicit
Sub systemofequations()
Dim A As Variant
Dim B As Variant
Dim X As Variant
ReDim A(1 To 2, 1 To 2)
ReDim B(1 To 2, 1 To 1)
With Sheet1
A(1, 1) = .Cells(1, 1)
A(1, 2) = .Cells(2, 1)
A(2, 1) = .Cells(4, 1)
A(2, 2) = .Cells(5, 1)
B(1, 1) = .Cells(3, 1)
B(2, 1) = .Cells(6, 1)
X = WorksheetFunction.MMult(WorksheetFunction.MInverse(A), B)
MsgBox "X equals " & X(1, 1) & " and Y = " & X(2, 1)
End With
End Sub
"X = WorksheetFunction.MMult(WorksheetFunction.MInverse(A), B)". Thanks.
Option Explicit
Sub systemofequations()
Dim A As Variant
Dim B As Variant
Dim X As Variant
ReDim A(1 To 2, 1 To 2)
ReDim B(1 To 2, 1 To 1)
With Sheet1
A(1, 1) = .Cells(1, 1)
A(1, 2) = .Cells(2, 1)
A(2, 1) = .Cells(4, 1)
A(2, 2) = .Cells(5, 1)
B(1, 1) = .Cells(3, 1)
B(2, 1) = .Cells(6, 1)
X = WorksheetFunction.MMult(WorksheetFunction.MInverse(A), B)
MsgBox "X equals " & X(1, 1) & " and Y = " & X(2, 1)
End With
End Sub