简体   繁体   中英

How to Use VBA to Loop goal seek down columns

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:

Excel表格

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.

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

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