Consulting

Results 1 to 5 of 5

Thread: GoalSeek by VBA

  1. #1
    VBAX Contributor
    Joined
    May 2010
    Posts
    106
    Location

    Red face GoalSeek by VBA

    Hi,

    Normally in formula or VBA code, we apply GoalSeek Goal:= NUMERICAL VALUE Ie. 0 or any numerical.

    How do I give a any cell reference to my goal numerical instead of manually write value?

    Reg, RL>

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim r As Long
      If Target.Count > 1 Or Target.Column <> 10 Then Exit Sub
      r = Target.Row
      Application.EnableEvents = False
      On Error Resume Next
      Range("H" & r).GoalSeek Goal:=Range("I" & r).Value, ChangingCell:=Range("A" & r)
      Application.EnableEvents = True
    End Sub

  3. #3
    VBAX Contributor
    Joined
    May 2010
    Posts
    106
    Location
    I do not understand this; In my sheet GoalSeek Goal in D14 and Changing Cell G7.

    Pls can you explain little, how I can apply!

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You did not provide enough information so I posted my best guess. My example is worksheet event code. As one selects a cell in column 10 in a row, it executed data from that row for a goal seek.

    The line that pertains to your need is:
    Range("H" & r).GoalSeek Goal:=Range("I" & r).Value, ChangingCell:=Range("A" & r)
    Since you did not say, I will assume that the value is set in A1. My code for you then becomes:
    Range("A1").GoalSeek Goal:=Range("D14").Value, ChangingCell:=Range("G7")
    Macro code from a recording would be close to this.

  5. #5
    VBAX Contributor
    Joined
    May 2010
    Posts
    106
    Location
    Yes, It work!
    Many Thanks!
    RL>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •