+ Reply to Thread
Results 1 to 9 of 9

VBA to goalseek across column

Hybrid View

  1. #1
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    VBA to goalseek across column

    Hi,

    I need a VBA to code to goalseek multiple cells to 0 by changing multiple cells.

    I have a range of set cells (EA44:GH44) to Value of 0 by changing cells (BP44:DW44). I have written a code but I am not sure if it works. I wanted to check before running the macro.

    Public Sub A()
      Dim iRow         As Long
      Dim iCol          As Long
    
      For iRow = 44
        For iCol = Columns("BP").Column To Columns("DW").Column
          With Cells(iRow, iCol + 63)
            .GoalSeek Goal:=0, ChangingCell:=.Offset(, -63)
          End With
        Next iCol
    End Sub
    Thanks

  2. #2
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: VBA to goalseek across column

    Looks correct.

    However, as with any macro/VBA codes you do, save the file first before running the macro. That way if something unexpected happens, you can revert to the last saved file.

  3. #3
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA to goalseek across column

    OK. If I wanted to achieve this with an iteration would this work:

    Sub Iterate ()
    Dim i as Integer
    For i = 1 to 100
    Application.Calculate
    Range("BP44:DW44").Value = Range ("EA44:GH44").Value
    Next i
    End Sub
    Is using an iteration faster than a goalseek. At the moment my spreadsheet takes a while to process. Is there a way to make it quicker or perhaps some VBA code?

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,939

    Re: VBA to goalseek across column

    I don't know that we can answer the question of which is faster based solely on the VBA code you have posted. Speeding up a calculation is usually about 1) removing bottlenecks from the calculation, 2) reducing the number of iterations needed to find a solution, 3) reducing the number of calculations/operations needed, and others.

    Goal Seek uses a Newton-Raphson type algorithm that is usually considered to be "faster" (in that it usually takes much fewer iterations) than a successive approximations type algorithm. Though, because of some of the overhead related to calling VBA and the Goalseek utility, I often find that successive approximations (with iteration turned on and appropriate circular references in place) will calculate faster. Based on your latest code, it looks like that is what you are simulating with your VBA code. If you are in a position to turn iteration on and go to the extra programming effort to make a good "circular reference", you may find that approach to be the easiest and most efficient.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: VBA to goalseek across column

    I agree with most of what MrShorty says above. It's hard to ascertain which method is faster without knowing a lot more.

    However, if I were to hazard a guess, the 2nd option (For Loop) would almost produce a faster solution as you may be able to utilise more of your processor as you are solving for 63 solutions at 1 go, rather than solving for each 1 at a time.

    Having said that, I would propose the following solution. Rather than use a For Loop where the number of iterations is fixed, I would propose using a Do Loop instead. That way, you only run enough iterations to achieve your objective, rather than running it 100 times when you only need 20 to solve.

    Sub Iterate ()
    Do Until Application.WorksheetFunction.Sum(Range("EA44:GH44")) = 0
    Range("BP44:DW44").Value = Range ("EA44:GH44").Value Application.Calculate
    Loop End Sub

  6. #6
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA to goalseek across column

    Hi,

    I have tried to use the VBA that you mentioned above and it comes up with a run time error 1004. "Unable to get the Sum property of the WorkSheetFucntion class."

    Why is this?

    The range EA44:GH44 are the cells that need to be goalseeked to 0.

    Thanks

  7. #7
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA to goalseek across column

    I have used the VBA code that I posed initially

    Public Sub A()
      Dim iRow         As Long
      Dim iCol          As Long
    
      For iRow = 44
        For iCol = Columns("BP").Column To Columns("DW").Column
          With Cells(iRow, iCol + 63)
            .GoalSeek Goal:=0, ChangingCell:=.Offset(, -63)
          End With
        Next iCol
    End Sub
    When I try and run it it comes up with a syntax error message box with the line highlighted in red.

    Can anyone help?

  8. #8
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: VBA to goalseek across column

    You don't need to loop for row numbers as you are only looking through 1 row.

    Just use
    iRow = 44
    For iCol = ...
    ...
    Next iCol

  9. #9
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA to goalseek across column

    That seems to work. Thanks for that.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Goalseek in VBA - Is it possible to see how many iterations were used?
    By Belisartih in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2014, 04:12 PM
  2. Formula Instead of Goalseek
    By Darreno in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-31-2014, 05:58 PM
  3. Similar to Goalseek
    By pizzle523 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-30-2012, 11:18 AM
  4. GoalSeek & Index + GUI
    By defy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-13-2012, 07:23 PM
  5. Problems in Goalseek
    By elizi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-26-2007, 06:38 AM
  6. GoalSeek in VBA
    By [email protected] in forum Excel General
    Replies: 1
    Last Post: 07-20-2006, 12:20 AM
  7. mimic goalseek
    By Monique in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-25-2005, 03:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1