如何在 VBA 循环中更改起始发票号码

如何在 VBA 循环中更改起始发票号码

尝试实现这个宏-每次打印时都会更改一个数字

我目前正在尝试在打印批次时使用宏来更改发票号码。

***Sub PrintCopies_ActiveSheet()
Dim CopiesCount As Long
Dim copynumber As Long

CopiesCount = Application.InputBox("How many copies do you want?", Type:=1)
'Now the program wants you to input how many pages you like to print.
'You can input 100 here.

For copynumber = 1 To CopiesCount
With ActiveSheet
   .Range("E1").Value = copynumber 'I assume your invoice number is in cell E1.
   .PrintOut 'Print the sheet

End With
Next copynumber
End Sub***

它按原样工作,但我想打印一批从发票号 400 开始的发票。当我尝试更改此部分时

For copynumber = 1 To CopiesCount

For copynumber = 400 To CopiesCount

它不起作用。(此更改是在对链接问题中已接受的答案的评论中建议的。)

我如何更改代码以便让我指定起始发票号码?

答案1

尝试这个修改后的代码。我添加了两个新变量(startlimit),使此代码更易于定制以打印发票号码。现在有第二个用户提示输入起始发票号码。limit是根据两个用户输入计算得出的。

Sub PrintCopies_ActiveSheet()
Dim CopiesCount As Long
Dim copynumber As Long
Dim start as Variant, limit As Long

CopiesCount = Application.InputBox("How many copies do you want?", Type:=1)
'Now the program wants you to input how many pages you like to print.
'You can input 100 here.

'starting invoice number
start = Application.InputBox("Start sequence at what invoice number?", Type:=1)  

'This gives you the ability to cancel the macro by clicking Cancel.
If start = "False" Then
    Exit Sub
End If

limit = start + CopiesCount - 1   'last invoice number to print


For copynumber = start To limit
    With ActiveSheet
       .Range("E1").Value = copynumber 'I assume your invoice number is in cell E1.
       .PrintOut 'Print the sheet

    End With
Next copynumber
End Sub

相关内容