简体   繁体   中英

Automatic Goal Seek Over Range of Cells

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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