Excel

Autonumber invoices in a shared environment

Ease of Use

Easy

Version tested with

2003 

Submitted by:

mdmackillop

Description:

This utility produces the next Record Number for invoices or the like where there is shared use of a file or template. 

Discussion:

Produce invoices etc with unique reference numbers. Numbers may be obtained on opening the document, by button click, or on Print or similar event. 

Code:

instructions for use

			

'//Code to be placed in a standard module Option Explicit Sub SetNo() 'Ament to suit file name and location Const Invoice = "C:\DataFile.txt" Const ForReading = 1, ForWriting = 2, ForAppending = 3 Dim fs, f, msg Set fs = CreateObject("Scripting.FileSystemObject") 'Read the last entry from the datafile Set f = fs.openTextFile(Invoice, ForReading, TristateFalse) Range("InvNo").Formula = f.readline f.Close 'Increment the number in the data file Set f = fs.openTextFile(Invoice, ForWriting, TristateFalse) f.write Range("InvNo").Formula + 1 f.Close End Sub Sub DoPrint() Application.EnableEvents = False If Range("InvNo") = "" Then SetNo On Error GoTo Finish ActiveWindow.SelectedSheets.PrintOut Copies:=InputBox("Print copies", "Print Invoice", 1) Range("InvNo").ClearContents Finish: Application.EnableEvents = True Application.ScreenUpdating = True End Sub Sub GetNumber() Application.EnableEvents = False If Range("InvNo") = "" Then SetNo Application.EnableEvents = True End Sub '//Code to be placed in a Worksheet module Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim Check As Long, NewCel As Range Set NewCel = ActiveCell Application.EnableEvents = False If Not Intersect(Target, Range("InvNo")) Is Nothing Then Check = MsgBox("Have you a reason to edit this box?", 292, "Edit Invoice Number") If Check = vbNo Then Application.Undo End If NewCel.Select Application.EnableEvents = True End Sub

How to use:

  1. Paste the code into module and worksheet functions
  2. Set a RangeName InvNo to hold the value and format as required.
  3. Create buttons as required and assign to the macros
  4. Create a text file to hold the autonumber values with an initial number and save in a suitable location.
  5. Change the path in the SetNo macro to suit the text file.
  6. The Template file may be saved either as a WorkGroup or a User template or both. As it does not store the autonumber data, the template and any workbooks created from it should update to the next available number.
 

Test the code:

  1. Copy the text file onto the C:\ root directory.
  2. Save the spreadsheet template to a suitable location and open new workbook based upon it; test button actions.
 

Sample File:

autonumber.zip 8.01KB 

Approved by mdmackillop


This entry has been viewed 274 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express