以下内容是记录Excel表格被打印次数的VBA源代码!
条件:
①使用A1单元格作为统计打印次数的数据;
②在表格中添加一个按钮控件,标题文字是“打印”;
此按钮控件的代码如下:
Private Sub CommandButton1_Click()
Dim i, PrintC, x As Integer
Dim CountN As String
PrintC = InputBox("打印份数") '弹出输入对话框
For i = 1 To PrintC '循环
ActiveSheet.PrintOut '打印工作表
CountN = Cells(2, 1) '从第1行,第1列的单元格中取得流水号
x = CInt(Right(CountN, 5)) '取流水号的右边5位,转换为整数,如果流水号变了,改动数位就可以
x = x + 1 '流水号+1
Select Case Len(CStr(x)) '用0补足不到5位的号码
Case 1
CountN = Left(CountN, 2) & "0000" & CStr(x)
Case 2
CountN = Left(CountN, 2) & "000" & CStr(x)
Case 3
CountN = Left(CountN, 2) & "00" & CStr(x)
Case 4
CountN = Left(CountN, 2) & "0" & CStr(x)
Case 5
CountN = Left(CountN, 2) & "" & CStr(x)
End Select
Cells(2, 1) = CountN '改变工作表里的流水号
Next
End Sub