问题
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