在每个按钮单击时增加excel中的行值

时间:2015-05-04 13:50:51

标签: excel vba row increment

在excel宏中

每次单击按钮时我都需要增加行。应从数据表中读取的Next值函数。此值也需要返回到不同工作表中的单元格。这是我的代码,如何增加每次点击的值。我尝试了Offset(),但我希望按钮能够记住我以前的位置。 For循环中的东西可能

Sub Button6_Click()

Cells(4, 6) = Sheets("Sheet4").Range("B:B").Value

End Sub

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题,但似乎您希望Cells(4,6)在每次点击时与Sheet4移位的某个单元格的值相等排下来。如果是这种情况:

Dim previousRow As Long '<-- global variable previousRow
Sub Button6_Click()
    If previousRow = 0 Then 
        previousRow = 1 '<--starts at row 1
    Else
        previousRow = previousRow + 1 '<--increases of 1 each click
    End If
    Sheets("Sheet1").Cells(4,6) = Sheets("Sheet4").Cells(previousRow,6) '<-- it will be the value of the cell in the column 6 with a variable row
    'first click: Cells(1,6)
    'second click: Cells(2,6)
    'third click: Cells(3,6)
    'etc.
End Sub