简体   繁体   中英

How to set goal seek value by a input box?

I have an excel file where I have to run the goal seek vba to achieve a certain number. But each time I need to change the goal seeking number manually if I run this in other cell. Is there any way of popping up a input box where i can put the required number and then the macro will run to accomplish the operation?

In the code below, goal seek is set to 40. but when i need the goal I have to change the number manually. i am expecting some extension that will require a goal seeking number each time I run the macro. Any idea please?

'
' Run_DIO Macro
'
' Keyboard Shortcut: Ctrl+q
'

    ActiveCell.GoalSeek Goal:=40, ChangingCell:=ActiveCell.Offset(-4, -1).Range( _
        "A1")
    ActiveCell.Offset(-3, -1).Range("A1").Select
    Selection.Copy
    ActiveCell.Offset(-1, 0).Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveCell.Offset(4, 2).Range("A1").Select
End Sub

Sure... just add a variable:

Dim iGoal As Integer
iGoal = InputBox("Enter Goal")
ActiveCell.GoalSeek Goal:=iGoal, ChangingCell:=ActiveCell.Offset(-4, -1).Range( _
    "A1")

Just hope they enter a number or you will get an error.

if you have it in a loop, get the input before the loop starts:

Dim iGoal As Integer
iGoal = InputBox("Enter Goal")

' code to start loop goes here

ActiveCell.GoalSeek Goal:=iGoal, ChangingCell:=ActiveCell.Offset(-4, -1).Range( "A1")

' also the rest of the code you want to loop goes here

' code to end loop goes here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM