I'm new to VB, but I've got some code that enable sequential numbers (like an
invoice number) to be printed on an excel form. It works well, but it starts
the first number at "0001", then "0002" and so-on. How can I get the first
number to be one of my choosing (say 5000)?
Here is my code:
Public Sub Workbook_Open()
Worksheets("invoice").Range("d1") = False
End Sub
' Disable the general sheet printing function so its all controlled by the
print button macro
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Worksheets("invoice").Range("D1") = False Then
Cancel = True
End If
End Sub
Then create a button on the worksheet with the code
Private Sub CommandButton1_Click()
Dim CopiesCount As Long
Dim CopieNumber As Long
Dim nNumber As Long
' Set the location of the registry entry to hold the sequential number
outside Excel
'use Start--> run--> regedit to open the registry
'registry location is 'Software-->VB and VDA Program
Settings-->Excel-->"Invoice"-->Invoice_Key"
'"Invoice" is the sheet name used in this example it needs to be your sheet
name.
'"invoice Key" is just a registry entry name - it can be anything you want
for your application
Const sAPPLICATION As String = "Excel"
Const sSECTION As String = "Invoice"
Const sKEY As String = "Invoice_key"
Const nDEFAULT As Long = 1&
nNumber = GetSetting(sAPPLICATION, sSECTION, sKEY, nDEFAULT) 'retreive the
last sheet number printed from
' the registry
' Ask the user how many copies of the template to print
'You can modify this line to say default print 100 copies or whatever you need
CopiesCount = Application.InputBox("How many Copies do you want to print?",
, 1, Type:=1)
For CopieNumber = nNumber To (nNumber + (CopiesCount - 1))
With ThisWorkbook.Sheets("Invoice")
With .Range("B2")
If IsEmpty(.Value) Then
nNumber = GetSetting(sAPPLICATION, sSECTION, sKEY, nDEFAULT)
.NumberFormat = "@"
.Value = Format(nNumber, "0000")
Else
.Value = Format(CopieNumber, "0000")
End If
End With
Worksheets("invoice").Range("D1") = True
'Print the sheet
.PrintOut
End With
Next
nNumber = CopieNumber
SaveSetting sAPPLICATION, sSECTION, sKEY, nNumber&
Worksheets("invoice").Range("D1") = False
End Sub
Thanks!
shah shailesh - 15 Jul 2007 09:31 GMT
Try this,
> Const nDEFAULT As Long = 1&
to
Const nDEFAULT As Long = 5000
Regards,
Shailesh Shah
http://in.geocities.com/shahshaileshs/
(Excel Add-ins Page)
If You Can't Excel with Talent, Triumph with Effort.
> I'm new to VB, but I've got some code that enable sequential numbers (like
> an
[quoted text clipped - 75 lines]
>
> Thanks!
AttackIP - 15 Jul 2007 17:16 GMT
That worked, I just had to change the registry name since it already had a
saved number of print entries in it from all my tests.
Thanks a bunch!!
JD
> Try this,
>
[quoted text clipped - 89 lines]
> >
> > Thanks!