Excel Weight Solver Script!

infraxo

New Member
Joined
Jul 6, 2011
Messages
7
I would like to make a weight solver, such that:

If I am given three values A B C and a fourth value D, the script will calculate the best approximate weights of A B and C that will give D.

For example, given

A = 10
B = 5
C = 2
and
D = 6.136

I would like the script to calculate approximations such as:

wA = 0.4
wB = 0.31
wC = 0.29

or indeed the exact answer (if it exists):

wA = 0.4
wB = 0.312
wC = 0.288

Please help me! Anything would be appreciated.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
This code users solver when a particular cell is changed

it also hides the solver dialog box

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, ActiveWorkbook.Names("bmweightEuro").RefersToRange) Is Nothing Then
 
    Range("E6").Select
    solverreset
    SolverOk SetCell:="$E$6", MaxMinVal:=3, ValueOf:="0.3", ByChange:="$D$4:$D$5"
    SolverAdd CellRef:=Range("D6"), Relation:=2, FormulaText:=1
    SolverAdd CellRef:=Range("D4"), Relation:=3, FormulaText:=0.1
    SolverAdd CellRef:=Range("D5"), Relation:=3, FormulaText:=0.1
    SolverSolve userfinish:=True

End If
end sub

Should get you started
 
Upvote 0
Check your References under Tools menu in VBA editor

is the SOLVER entry ticked?

also I'm checking a named Range called "bmweightEuro" which you won't have so you have to adapt it
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,706
Members
452,939
Latest member
WCrawford

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