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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
try something like this (This goes in your workbook module)
Code:
Public N9 As String
Private Sub Workbook_Open()
    N9 = range("N9")
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As range)
    If range("N9") <> N9 Then 
        Goalseek123
    End If
End Sub

ALSO, don't forget to add Application.EnableEvents = False to the beginning of your Goalseek123 and Application.EnableEvents = True to the end of it.
 
Upvote 0
You mean this?

********************
Sub Goalseek123()

Application.EnableEvents = False

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")

Application.EnableEvents = True


End Sub
 
Upvote 0
No it didn't :(

I have the exactly the followings:
Public N9 As String
Private Sub Workbook_Open()
N9 = Range("N9").Value
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Range("N9") <> N9 Then
Goalseek123
End If
End Sub

What do you mean by Module? The above code is not shown as an individual module, but shown when I click Sheet3 under Microsoft Excel Objects.

Do I need to create a new module under Modules folder?
 
Upvote 0
when I type MsgBox in front of Goalseek123, it gives the error message "compile error" again :(

As it should.

I meant like this:

Code:
Private Sub...
  .Range(N9)...
  msgbox "foo"
  ' everything else
end sub

You just use the message box as a quick way of testing if the code is firing or not, but it needs to be on its own line.
 
Upvote 0
Put this in the "ThisWorkbook" module
Code:
Public N9 As String
Private Sub Workbook_Open()
    N9 = Sheets(3).range("N9").Value
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As range)
    If Sheets(3).range("N9") <> N9 Then Debug.Print "N9 has changed since the workbook opened"
End Sub
 
Upvote 0
If N9 is the result of a calculation, a change event won't do anything. You'd need to change it to a Calculate event instead.

Code:
Private Sub Worksheet_Calculate()
  If Range("N9") = Somthing then Goalseek123
End sub
 
Upvote 0
Not working :banghead::banghead::banghead:

I have the codes you just typed under "ThisWorkbook" under the folder "Microsoft Excel Objects"

There is another folder named with "Modules" but under this folder there is no "ThisWorkbook".

:confused:
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,741
Members
452,940
Latest member
rootytrip

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