简体   繁体   English

要求一个目标并使用给定的数字运行 Goal Seek

[英]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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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