goal seek using vba ?

spurs

Active Member
Joined
Oct 18, 2006
Messages
479
Office Version
  1. 2016
  2. 2013
  3. 2010
  4. 2007
  5. 2003 or older
Platform
  1. Windows
I have a macro that calculates a function. There re 3 inputs to the function

Often I use the excel spreadsheet to do a goal seek to set the result of that faction by allowing one of the inputs to vary

Is there a simple way to program this in vba or do I basically have to write a complete reverse function from scratch?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Yes, and it's a very useful trick which I've used in some really powerful analytical tools

By way of example, I've entered the values 1, 2, 3 in cells A1:A3, then A4 formula "=sum(A1:A3)". The following code places the values 5, 6, 7 in cells B1:B3, being the changes required to each number in order to sum to 10
Code:
Option Explicit

Sub goalSeekExample()
    
Dim cl As Range, dblOriginal As Double
For Each cl In Range("A1:A3")
    
    dblOriginal = cl.Value                              ' store original value
    Range("A4").GoalSeek Goal:=10, ChangingCell:=cl     ' change each cell, one at a time, so A4 sum result = 10
    cl.Offset(0, 1) = cl.Value                          ' store goal seek result in adjacent cell
    cl.Value = dblOriginal                              ' reset value
    
Next cl


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,891
Messages
6,181,614
Members
453,057
Latest member
LE102024

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