📜  每次打印时excel自动递增编号 - 无论代码示例

📅  最后修改于: 2022-03-11 14:59:05.840000             🧑  作者: Mango

代码示例1
Public Sub IncrementPrint()
    Dim resp As Variant, scr As Boolean, i As Long, j As Long

On Error Resume Next
    resp = Application.InputBox(Prompt:="Please enter the number of copies to print:", _
                                Title:="Select Total Print Copies", Type:=1)
On Error GoTo 0

    If resp = False Then Exit Sub
    If resp < 1 Or resp > 100 Then
        MsgBox "Invalid number: " & resp & " (Enter 1 to 100)", vbExclamation, "Try Again"
        Exit Sub
    End If


On Error Resume Next
    StartValue = Application.InputBox(Prompt:="Please enter start number:", _
                                Title:="Start number", Type:=1)
On Error GoTo 0

    If StartValue = False Then Exit Sub
    If StartValue < 1 Or resp > 10000 Then
        MsgBox "Invalid number: " & StartValue & " (Enter 1 to 10000)", vbExclamation, "Try Again"
        Exit Sub
    End If

    scr = Application.ScreenUpdating
    Application.ScreenUpdating = False
    
    j = 0
    For i = 1 To resp
        ActiveSheet.Range("C2").Value2 = i + 0 + j + StartValue - 1
        ActiveSheet.Range("K2").Value2 = i + 1 + j + StartValue - 1
        ActiveSheet.Range("C21").Value2 = i + 2 + j + StartValue - 1
        ActiveSheet.Range("K21").Value2 = i + 3 + j + StartValue - 1
        ActiveSheet.PrintOut
        j = j + 3
    Next i
    ActiveSheet.Range("C2,K2,C21,K21").ClearContents
    Application.ScreenUpdating = scr
End Sub