循环代码的单元格

时间:2013-11-05 14:13:08

标签: excel vba

我正在学习VBA,并希望在excel 2007中循环或更改例如cell-1的值,直到cell-2等于cell-3。我已经制作了一个宏和一个按钮来激活它,但我正在努力使代码执行此操作。我不希望代码激活求解器我希望有一段时间直到循环才会这样做。有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

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

如果您有疑问,请告诉我。