I have a spreadsheet where I have been able to create and assign a macro:
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:
I changed it to:
(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.
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.