Excelmacrosbutton更改更改单元格值

任何想法如何让一个button更改基于表的单元格值。 说单元格A1是活动单元格,我希望这个值每次都改变我点击基于列B中的值的button,只是沿着列表B1,B2,B3等…

Private Sub CommandButton1_Click() Range("A1").Value = Range("B1:B10").Value End Sub 

我假定你想要返回到B1值,一旦你达到了B10的价值和另一个点击进行。

 Private Sub CommandButton1_Click() If IsError(Application.Match(Range("A1").Value, Range("B1:B10"), 0)) Then Range("A1").Value = Range("B1").Value ElseIf Application.Match(Range("A1").Value, Range("B1:B10"), 0) = Range("B1:B10").Cells.Count Then Range("A1").Value = Range("B1").Value Else Range("A1").Value = Range("B1").Offset(Application.Match(Range("A1").Value, Range("B1:B10"), 0), 0).Value End If End Sub 

如果你有兴趣使用助手单元,那么你可以使用下面的代码。

  Private Sub CommandButton1_Click() Dim increment As Long increment = Range("XFD1048576").Value Range("A1").Value = Range("B1").Offset(increment, 0).Value Range("XFD1048576").Value = increment + 1 If Range("XFD1048576").Value >= Range("B:B").End(xlDown).Row Then Range("XFD1048576").Value = 0 End If End Sub