每个按钮单击增加行

时间:2013-07-10 06:13:11

标签: excel vba

我希望每次点击都可以将单元格值更新为另一个工作表中的下一个单元格值,然后打印该值。

Sub SerPrint()

i = 1

'when button click it change to `c3 c4 c5 and C......
' until the end of table
Sheet1.Range("n7").Value = Sheet2.Range("C"2 + i )
'like Value in cell N7 from sheet1 have value from c2 Sheet2 then
' after i click it change to c3,c4,c5,c....

Sheet1.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
' then print out

End Sub

2 个答案:

答案 0 :(得分:2)

每次执行sub时,都需要添加公共变量来控制行的偏移量。

将此行添加到项目中任何模块的顶部

Public iOffset as Long

你的子将会是这样的:

Sub SerPrint()

'let's start with value of C2 from Sheet2
Sheet1.Range("n7").Value = Sheet2.Range("C2").Offset(iOffset, 0)

Sheet1.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
' then print out

'here we increment our public variable for next click to make C3, C4...
iOffset = iOffset + 1


End Sub

答案 1 :(得分:1)

以你的风格..

Sheet1.Range("n7").Value = Sheet2.Range("C" & format(2 + i) )

如果你想使用Offset ..

Sheet1.Range("n7").Value = Sheet2.Range("C2").Offset(i,0)