简体   繁体   English

如何使用VBA循环目标向下搜索列

[英]How to Use VBA to Loop goal seek down columns

I am new to this site and brand new to VBA. 我是这个网站的新手,也是VBA的新手。 I have a question related to goal seek. 我有一个与目标寻求有关的问题。

I need to use Goal seek in VBA several times. 我需要在VBA中多次使用目标搜索。 Here is a sample of my Excel Sheet: 这是我的Excel工作表示例:

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(硬编码)中的单元格来将F(与另一个工作表链接的公式)中的单元格设置为0。 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". 我有执行此操作的基本代码,但是我想弄清楚如何告诉Excel“嘿,为列中的每个单元格循环执行此操作”。 I don't want to have to manually put this command in VBA hundreds of times. 我不想在VBA中多次手动输入此命令。 Here is what I actually have in VBA. 这是我实际上在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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