在Excel VBA模块中使用GoalSeek功能

时间:2015-08-29 13:17:33

标签: excel vba excel-vba

任何人都可以帮助我理解为什么第一个子工作但第二个子工程提供无效的参考?

Sub tester1()

    Range("Q5").GoalSeek goal:=2000000, changingcell:=Range("P5")

End Sub


Sub tester3()
    Dim rngRow As Range
    For Each rngRow In Range("P5").Rows
        rngRow.Cells(0, 1).GoalSeek goal:=2000000, changingcell:=rngRow.Cells(0, 0)
    Next rngRow
End Sub

提前致谢。

1 个答案:

答案 0 :(得分:1)

Cells(0, 1)Cells(0, 0)无效 - 行和列应大于0

Range("A1") = Cells(1, 1)    'Cells(row, col)
Range("A2") = Cells(2, 1)
Range("B1") = Cells(1, 2)
Range("B2") = Cells(2, 2)

用此测试:

Sub tester3()

    Dim cel As Range

    For Each cel In Range("Q5:Q10")

        cel.GoalSeek Goal:=2000000, ChangingCell:=cel.Offset(0, -1)

    Next rngRow

End Sub