Forum Discussion
Chamara Wickramasinghe
Jun 17, 2018Copper Contributor
Increase number when saving
I got an Excel template to input Invoice details and save. It has the Invoice Number in one cell and my requirement is to auto generate it when open the template. See the following directory. e.g: C...
Matt Mickle
Jun 18, 2018Bronze Contributor
Hey Chamara-
Hope your doing well. Yes, this is possible with a little VBA Code:
You will need a workbook open event that triggers when the workbook is opened as well as a beforesave event that triggers before save as the naming convention denotes. If your Invoice number is in cell A1 for example then you would want to do something like this:
This code needs to be put in the ThisWorkbook Code Module:
Private Sub Workbook_Open()
Range("A1").Value = Range("A1").Value + 1
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.EnableEvents = False
rtn = Application.Dialogs(xlDialogSaveAs).Show(arg1:="My FileName Invoice " & Range("A1").Value)
Application.EnableEvents = True
If Not rtn Then Cancel = True
End Sub