How to automatically triger Goalseek macro?

yichuansancun

Board Regular
Joined
Feb 7, 2011
Messages
123
I have the following code:

Sub Goalseek()

Range("$N$7").Select
Range("$N$7").Goalseek Goal:=Range("$N$9").Value, ChangingCell:=Range("$N$3")

End Sub


How can I make excel to run this macro automatically every time there is a change in cell N9's value?

Thanks,
Perri
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$N$9" Then
'Your Macro Here
End If
End Sub

This goes in the Worksheet Code - Right click on the worksheet name and click "View Code" then paste this in.
 
Upvote 0
I input the following code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$N$9" Then
Macro Goalseek
End If
End Sub


it gave me the following error:
Compile Error
Expected Function or Variable


and "Goalseek" is highlighted when the error message appears.

what did I do wrong? :(
 
Upvote 0
Take out Macro and just use "Goalseek".

Note that it would be a good idea to change the name of the macro, so it's not the same as the method you're invoking.

HTH,
 
Upvote 0
Thanks, now I have the following codes:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$N$9" Then
Goalseek123
End If
End Sub


The macro Goalseek123 works fine if I trigger it. But the above code is not working. When there is a change in cell N19, the macro Goalseek 123 is not triggered.

:confused:

Any thoughts?

Thanks much!

Perri
 
Upvote 0
Is it in the proper module?

What happens if you go into the Immediate window and type ?Application.EnableEvents ?

You might also add a MsgBox before Goalseek123 and see if you get anything.
 
Upvote 0
when I type MsgBox in front of Goalseek123, it gives the error message "compile error" again :(

I have the macro Goalseek123:
Sub Goalseek123()

If LCase(Range("education_selection").Value) Like "*mercer data*" Then
Range("education").ClearContents
End If

If LCase(Range("choice").Value) Like "*specified amount*" Then
Range("housing_selection").ClearContents
ElseIf LCase(Range("choice").Value) Like "*mercer data*" Then
Range("housing").ClearContents
End If

Range("$N$7").Select
Range("$N$7").Goalseek Goal:=Range("$N$9").Value, ChangingCell:=Range("$N$3")

End Sub


How can I write a code that will trigger this macro if there is change in value in N9?

Sorry for the back and forth... but I cannot figure it out :confused:

Thanks much!
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,734
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