Automatic Goal Seek Over Range of Cells

吃可爱长大的小学妹 提交于 2020-01-07 05:44:12

问题


I want to apply goal seek across several rows when there is a change to any cell in the work sheet. I want to apply this from row 7 to row 11. The first problem I have is that excel is crashing each time I run this. I am just starting to learn VBA so any help is much apreciated. Thank you!

My code is below:

Option Explicit

Private Sub Worksheet_Calculate()
CheckGoalSeek
End Sub

Private Sub CheckGoalSeek()
Range("T7").GoalSeek Goal:=0, ChangingCell:=Range("V7")
End Sub

回答1:


You appear to be triggering an infinite loop: worksheet calculation -> goal seek calculation -> worksheet calculation -> ...

One option is to change the event that triggers the goal seek.

I would recommend the Worksheet_Change event. The event code would be the same except for the sub declaration, which would be Private Sub Worksheet_Change(ByVal Target As Range).

A simple For loop will perform the Goal Seek on the different rows:

 Option Explicit

 Private Sub CheckGoalSeek()
    Dim i as Long
    For i = 7 to 11
        Range("T"& i).GoalSeek Goal:=0, ChangingCell:=Range("V"& i)
    Next
 End Sub


来源:https://stackoverflow.com/questions/16240176/automatic-goal-seek-over-range-of-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!