Create input box in VBA for Goal Seek

eh9879a

New Member
Joined
Aug 5, 2014
Messages
2
I'm having trouble with my code for goal seek. It should be asking for input for the goal cell, but taking ranges for the other inputs. My cell G5/y has a formula. Using GoalSeek outside of VBA works perfectly. But using this code, I get a 1004 error "Reference Not Valid". The error highlights this line in yellow: "y.goalseek goal:=Z, ChangingCell:=x". VBA also changes the capitalization from y.GoalSeek Goal:=Z to all lowercase. Don't know why.

I'm new to VBA and mostly trying to learn from forums like this and trial and error. Any basic tips would be really appreciated in addition to figuring out why this won't run.

Code:
Sub GoalSeekVBA()

Dim x As Range
Dim y As Range
Dim Z As Range


Set x = Range("I6")
Set y = Range("G5")
Z = Application.InputBox("Please Enter Target Income", "Goal Seek Value")


y.goalseek goal:=Z, ChangingCell:=x


End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Still have the same error...I simplified the code to this though:
Code:
Sub GoalSeekVBA()

Dim z As Integer


z = Application.InputBox("Please Enter Target Income", "Goal Seek Value")


Range("G5").goalseek goal:=z, ChangingCell:=Range("I6")


End Sub
 
Upvote 0
Use long. Depending on the value you are inputting, you are probably exceeding the capacity for integer.

what value are you typing in?
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

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