简体   繁体   中英

Ask for a goal and run Goal Seek with given number

The code is

Sub GoalSeek()
'
' GoalSeek Macro
' Goal seek with input box for ending EBIT
'
'
Dim EBIT As String
    EBIT = InputBox("Enter ending EBIT goal in Millions", "Ending EBIT", "e.g. 140")
    Range("I56").GoalSeek Goal:=EBIT, ChangingCell:=Range("G52")
    VBA.MsgBox ("The number of years to obtain an EBIT of" & response & "is" & Range("G52"))
End Sub

Any help would be appreciated.

This could be what you want. Be sure to check the proper values to pass to the function. Create an error-handling routine if necessary.

Option Explicit

Function fnGoalSeek( _
    ByRef rngGoalSeek As Excel.Range, _
    ByRef rngChangingCell As Excel.Range, _
    ByVal lngGoal As Long)
    rngGoalSeek.GoalSeek Goal:=lngGoal, ChangingCell:=rngChangingCell
End Function

Sub fnRun_fnGoalSeek()
    Dim intYears As Long
    Dim lngEBIT As Long

    lngEBIT = InputBox("Enter ending EBIT goal in Millions", "Ending EBIT", "e.g. 140")

    Call fnGoalSeek(Range("I56"), Range("G52"), lngEBIT)
    intYears = Range("G52").Value
    
    MsgBox "The number of years to obtain an EBIT of " & lngEBIT & " is " & intYears
    
End Sub

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