列印後增加 4 個不同單元格中的連續數字

列印後增加 4 個不同單元格中的連續數字

我創建了一張1/4 信紙大小的收據,並將其在一個工作表中複製了4 次(以節省紙張),並希望它從001 到100 連續打印。號001,002...100..

如何將連續數字放入 4 個不同的單元格中,假設 A1、C1、E1 和 G1 是我的單元格,編號為 001,002,003,004,並在每次列印後增加每個數字?

我還需要能夠指定起始編號。

我在網路上找到了這個,所以也許這可以是一個開始:

Sub IncrementPrint()

    Dim xCount As Variant
    Dim xScreen As Boolean
    Dim I As Long
    On Error Resume Next
LInput:
    xCount = Application.InputBox("Please enter the number of copies you want to print:", "Title")
    If TypeName(xCount) = "Boolean" Then Exit Sub
    If (xCount = "") Or (Not IsNumeric(xCount)) Or (xCount < 1) Then
        MsgBox "error entered, please enter again", vbInformation, "Title"
        GoTo LInput
    Else
        xScreen = Application.ScreenUpdating
        Application.ScreenUpdating = False
        For I = 1 To xCount
            ActiveSheet.Range("A1").Value = " Company-00" & I
            ActiveSheet.PrintOut
        Next
        ActiveSheet.Range("A1").ClearContents
        Application.ScreenUpdating = xScreen
    End If
End Sub

答案1

用這個


Option Explicit

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

    scr = Application.ScreenUpdating
    Application.ScreenUpdating = False
    j = 0
    For i = 1 To resp
        ActiveSheet.Range("A1").Value2 = " Company-00" & i + 0 + j
        ActiveSheet.Range("C1").Value2 = " Company-00" & i + 1 + j
        ActiveSheet.Range("E1").Value2 = " Company-00" & i + 2 + j
        ActiveSheet.Range("G1").Value2 = " Company-00" & i + 3 + j
        ActiveSheet.PrintOut
        j = j + 3
    Next i
    ActiveSheet.Range("A1,C1,E1,G1").ClearContents
    Application.ScreenUpdating = scr
End Sub

相關內容