How to Use VBA to Loop goal seek down columns

匿名 (未验证) 提交于 2019-12-03 01:40:02

问题:

I am new to this site and brand new to VBA. I have a question related to goal seek.

I need to use Goal seek in VBA several times. Here is a sample of my Excel Sheet:

I need to set the cells in F(which is a formula linked to another sheet) to 0, by changing the cells in I (hardcoded). I have the basic code to do this, but I want to figure out how to tell Excel "hey, loop this action for every cell in the column". I don't want to have to manually put this command in VBA hundreds of times. Here is what I actually have in VBA.

Sub Goal_Seek() ' ' Goal_Seek Macro      Range("F7").Select     Range("F7").GoalSeek Goal:=0, ChangingCell:=Range("I7")     Range("F8").Select     Range("F8").GoalSeek Goal:=0, ChangingCell:=Range("I8")      Range("F9").Select     Range("F9").GoalSeek Goal:=0, ChangingCell:=Range("I9")      Range("F10").Select     Range("F10").GoalSeek Goal:=0, ChangingCell:=Range("I10")      Range("F11").Select     Range("F11").GoalSeek Goal:=0, ChangingCell:=Range("I11") End Sub 

Any help is appreciated.

回答1:

Maybe something like:

Sub Goal_Seek()     Dim lastRow As Long, i As Long      With ActiveSheet         lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row          For i = 7 To lastRow             .Range("F" & i).GoalSeek Goal:=0, ChangingCell:=.Range("I" & i)         Next i     End With End Sub 


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