我正在学习 VBA,并希望在 excel 2007 中循环或更改例如 cell-1 的值,直到 cell-2 等于 cell-3。我制作了一个宏和一个按钮来激活它,但我正在努力编写代码来执行此操作。我不希望代码激活求解器,我希望有一段时间直到循环完成。有任何想法吗?
谢谢你。
尝试这个:
Private Sub ChangeValueTilItsGoodEnough()
Dim r1 as Range, r2 as Range, r3 as Range
With ThisWorkbook.Sheet("Sheet1")
'define ranges
r1 = .Range("A1")
r2 = .Range("A2")
r3 = .Range("A3")
'loop until the two values are equal - may want to add some sort of checker
'to make sure an infinite loop doesn't happen. I'm assuming you're using
'a linear equation tool like Solver.
While (r2.Value <> r3.Value)
'change the value of r1
r1.Value = r1.Value + 1
Wend
End With
End Sub
如果您有任何问题,请告诉我。