Tricky(?) simultaneous equations in VBA

Mr. Numbers

New Member
Joined
Nov 19, 2011
Messages
2
Hi!

I have successfully written a macro to solve a set of simultaneous equations, as follows:

a + b + c = 24
a - b = 2
b - c = 2


My macro is fine:

Dim ValInv As Variant
Dim EqResults As Variant
Dim EqVal(1 To 3, 1 To 3) As Double
Dim EqGive(1 To 3, 1 To 3) As Double

EqVal(1, 1) = 1
EqVal(1, 2) = 1
EqVal(1, 3) = 1
EqGive(1, 1) = 24

EqVal(2, 1) = 1
EqVal(2, 2) = -1
EqVal(2, 3) = 0
EqGive(2, 1) = 2

EqVal(3, 1) = 0
EqVal(3, 2) = 1
EqVal(3, 3) = -1
EqGive(3, 1) = 2

Set WF = WorksheetFunction
ValInv = WF.MInverse(EqVal)
EqResults = WF.MMult(ValInv, EqGive)

Cells(1, 1) = EqResults(1, 1) 'This is a
Cells(2, 1) = EqResults(2, 1) 'This is b
Cells(3, 1) = EqResults(3, 1) 'This is c


I get a=10, b=8, c=6

"So what's the problem?" I here you ask. Well, suppose one of the calculations were multiplication, as shown below:

a + b + c = 22
a - b = 10
b * c = 16

The answer should be: a=12, b=2, c=8

However, I have NO idea how to accomplish this. Or am I just being so stupid and the answer is obvious? Running the matrices doesn't seem to be the answer.

Thanks for anyone who takes their precious time to read my query.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hello

Could you use the Excel Solver?
 
Upvote 0
Hello Wigi

First, I'm sorry if my title was a little vague - this is the first time I've ever put a question on a forum (I've been trying so hard to make a huge macro myself over the last 2 years, using books or other net info), and I'm no professional.

I'll look into Excel Solver. I had noticed it before, but thought it was something which could only be accessed 'manually' (i.e. entering data into a window) and not called like a function from within a macro.

Give me a week to try to make something.

I REALLY appreciate your post.
 
Upvote 0

Forum statistics

Threads
1,215,978
Messages
6,128,065
Members
449,417
Latest member
flovalflyer

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