Goal Seek to change automatically

anjenaire

New Member
Joined
Jan 18, 2006
Messages
8
I have a spreadsheet where I have been able to create and assign a macro:

VBA Code:
Sub Goal_Seek_TargetValue()
    Range("SetCell").GoalSeek _
        Goal:=Range("TargetValue"), _
        ChangingCell:=Range("ChangeCell")
        
    End Sub

Where TargetValue is the name of the cell containing the target value of the Goal Seek, ChangeCell is a named cell that is the Changing Cell, and SetCell is a named cell that is the Setting cell. Pretty straight forward and I can call the macro or use a button to execute the macro. What I would like to do is automatically run the macro if the target cell changes. It is currently set to the equal another cell that gets its value from an average of a number of other values. I found this VBA code that was supposed to do that but I don't get it to work correctly:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim inputCells As Range

'List all the input cells.
Set inputCells = Range("SalesUnits, SalesPrice, VariableCostPrice, FixedCost, " & _
    "TargetValue, SetCell, ChangeCell")

'Run the macro if an input cell changes
If Not Application.Intersect(Range(Target.Address), inputCells) Is Nothing Then

    'Run the Goal Seek using the values in the SetCell, TargetValue 
    'and ChangeCell named ranges
    Range(Range("SetCell").Value).GoalSeek Goal:=Range("TargetValue").Value, _
        ChangingCell:=Range(Range("ChangeCell").Value)

End If

End Sub

I changed it to:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim inputCells As Range

'List all the input cells.
Set inputCells = Range("SalesUnits, SalesPrice, VariableCostPrice, FixedCost, " & _
    "TargetValue, SetCell, ChangeCell")

'Run the macro if an input cell changes
If Not Application.Intersect(Range(Target.Address), inputCells) Is Nothing Then

    'Run the Goal Seek using the values in the SetCell, TargetValue
    'and ChangeCell named ranges
    Range("SetCell").GoalSeek Goal:=Range("TargetValue"), _
        ChangingCell:=Range("ChangeCell")

End If

End Sub

(I had to name the SalesUnits, SalesPrice etc. cells but those don't do anything on my sheet. When I tried to take them off the code stopped and until I made the changes I made in the second section, it also gives me an error.) I don't really know VBA at all, but I was hoping someone could give me a few pointers.

Thanks.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,214,552
Messages
6,120,172
Members
448,948
Latest member
spamiki

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