Results 1 to 3 of 3

Thread: Goal Seek Iteration Code in VBA? Possible?

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Unhappy Goal Seek Iteration Code in VBA? Possible?

    Hi there,

    I have an iteration which I want to use the principle of goal seek totally in vba without using the in-built excel one. Is this at all possible?

    Code:
    Sub Iterate()
    Dim row As Integer
    Dim i As Integer
    Dim temp As Single
    Dim flowCo As Single
    Dim flowExp As Single
    Dim PressCo As Single
    Dim OJ As Single
    Dim total As Single
    
    Dim X As Single
    
    R = 100
    
    '3.441062516
    Do Until total < 0.01 And total > 0
        'First need to calculate the O(J) Column
        row = Me.ListBox1.ListCount
        total = 0
        For i = 0 To row - 1
        temp = Me.ListBox1.List(i, 5)
        Me.ListBox1.List(i, 6) = R + temp
        
        'Now we calculate F(J)
        flowCo = Me.ListBox1.List(i, 2)
        flowExp = Me.ListBox1.List(i, 3)
        PressCo = Me.ListBox1.List(i, 4)
            
        OJ = Me.ListBox1.List(i, 6)
        Me.ListBox1.List(i, 7) = flowCo * ((Abs(OJ) ^ flowExp) * (OJ / Abs(OJ)))
        total = total + Me.ListBox1.List(i, 7)
        Next
    
    If total < 0 Then
    R = R + 0.1
    Else
        If total > 1 Then
        R = R ^ 0.5
        ElseIf total > 0.1 Then
        R = R - 0.1
        ElseIf total > 0.01 Then
        R = R - 0.01
        End If 
    End If
    
    
    Loop
    
    MsgBox R
    
    'Now we need to sum it up
    
    
    
    End Sub
    I have tried all kinds of Tweaks and it does not seem to work

    please help!
    If you find my thread helpful, please remember to rate me

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Goal Seek Iteration Code in VBA? Possible?

    Perhaps you can adapt the code from this:

    http://www.bmsltd.ie/DLCount/DLCount...e=GoalSeek.zip

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Goal Seek Iteration Code in VBA? Possible?

    Or you could just call the GoalSeek method of the target cell.

    Excel Developer Reference
    Range.GoalSeek Method
    Calculates the values necessary to achieve a specific goal. If the goal is an amount returned by a formula, this calculates a value that, when supplied to your formula, causes the formula to return the number you want. Returns True if the goal seek is successful.expression.GoalSeek(Goal, ChangingCell)
    Return Value
    Boolean

    expression A variable that represents a Range object.

    Parameters

    Name Required/Optional Data Type Description
    Goal Required Variant The value you want returned in this cell.
    ChangingCell Required Range Specifies which cell should be changed to achieve the target value.

    Example


    This example assumes that Sheet1 has a cell named "Polynomial" that contains the formula =(X^3)+(3*X^2)+6 and another cell named "X" that’s empty. The example finds a value for X so that Polynomial contains the value 15.

    Visual Basic for Applications
    Worksheets("Sheet1").Range("Polynomial").GoalSeek _
    Goal:=15, _
    ChangingCell:=Worksheets("Sheet1").Range("X")
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width