Marc Chevalier
asked on
VBA code in Excel to increment invoice number and save the workbook as "Invoice" + "InvNo"
I had some VBA code in Excel and lost it, I will spare you the details. I need this code again. I need code that will increment the Invoice number which is saved in C:\Invoices\InvNo.txt and populate the next InvNo in the spreadsheet in cell F2 when opening the spreadsheet. I have been able to do this and replicate it and it works. Here is the part I can't remember. I need to save each worksheet or Invoice as Invoice9999.txt in C:\Invoices\Invoice9999.tx t where the 9999 portion is the same InvNo that was used to populate cell F2 when opening. The end result is that the last InvNo is stored locally on the hard drive as well as the worksheet with the appropriate data for that InvNo. Over time, the local directory should have all the Invoices created stored as txt files with the InvNo showing in the name. Thanks for any help you can provide. The InvNo gets incremented at the opening of the spreadsheet and the file should be saved as a txt file when the spreadsheet is saved after data entry is completed. Thank you very much.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
As an alternate method, instead of recording multiple files that reference the most recent reference number, here is a question I contributed to in 2003:
"get data from a file, increment and save back to file"
[ https://www.experts-exchange.com/questions/20742111/get-data-from-a-file-increment-and-save-back-to-file.html?anchorAnswerId=9391947#a9391947 ]
Here I provided a routine to return the "next invoice number" (that could be returned to cell [F2], for example).
To achieve this, a file ("h:\invoice.txt") is maintained that is initially read & the previously recorded value stored is obtained. This value is returned, & the value within the file is updated by a value of 1 (one).
The file, therefore, always stores the "next invoice number".
Cell [F2] would contain the formula: =Get_Next_Invoice_Number()
It would be a simple task to amend this routine to read the file, increment by one, update the file, then return the incremented value instead.
The file would then contain the "current invoice number".
The filename could also be amended to a name like you originally intended ("Invoice{Number}.txt"), if desired.
Would you require multiple files to be maintained within the same folder if you know that the only file that exists in the pre-defined location includes the most recent (or "next") value?
(Either of the two proposals provided above could be amended to remove "old" files when a new file is created, for instance).
Either way, there is further discussion in the original thread if you wish to look at the other suggestions made at that time.
BFN,
fp.
As an alternate method, instead of recording multiple files that reference the most recent reference number, here is a question I contributed to in 2003:
"get data from a file, increment and save back to file"
[ https://www.experts-exchange.com/questions/20742111/get-data-from-a-file-increment-and-save-back-to-file.html?anchorAnswerId=9391947#a9391947 ]
Here I provided a routine to return the "next invoice number" (that could be returned to cell [F2], for example).
To achieve this, a file ("h:\invoice.txt") is maintained that is initially read & the previously recorded value stored is obtained. This value is returned, & the value within the file is updated by a value of 1 (one).
The file, therefore, always stores the "next invoice number".
Cell [F2] would contain the formula: =Get_Next_Invoice_Number()
It would be a simple task to amend this routine to read the file, increment by one, update the file, then return the incremented value instead.
The file would then contain the "current invoice number".
The filename could also be amended to a name like you originally intended ("Invoice{Number}.txt"), if desired.
Would you require multiple files to be maintained within the same folder if you know that the only file that exists in the pre-defined location includes the most recent (or "next") value?
(Either of the two proposals provided above could be amended to remove "old" files when a new file is created, for instance).
Either way, there is further discussion in the original thread if you wish to look at the other suggestions made at that time.
BFN,
fp.
You're welcome.
Open in new window