I made a form in excel in which the cell O2 is a invoice number. This number increase by 1 everytime the document is open so each new invoice have a unique number.
To achieve this I put this VBA code directly in ThisDocument :
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("O2") = Worksheets("Sheet1").Range("O2") + 1
ActiveWorkbook.Save
End Sub
What this do is that everytime the master document (the empty form) is open it add +1 to the invoice number and immediately save the document so the next time this empty form is opened it will start from the new number and add + and so on. It works great.
At the end of the form there is a "Save" button which automatically save the completed forms in the same folder as the master empty document but using the new invoice number in O2 as a file name. Then automatically close the document so, the user will have to reopen a new empty form to make a new invoice. My code for this button is :
Sub SaveAsO2()
InvoiceNo = Range("O2").Value
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & Application.PathSeparator & "FL" & InvoiceNo & ".xlsm"
ActiveWorkbook.Close
End Sub
Everything works really great except for one thing. This newly saved invoice will have is invoice number in O2 increase by 1 too if we open it in the futur since it keep all the macro active.
So what I want is adding a line to my Save button which delete the code in ThisWorkbook before saving it.
Is it something possible? I'm open to any other way I can achieve what I want if you have a suggestion.
Thanks
Alex
To achieve this I put this VBA code directly in ThisDocument :
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("O2") = Worksheets("Sheet1").Range("O2") + 1
ActiveWorkbook.Save
End Sub
What this do is that everytime the master document (the empty form) is open it add +1 to the invoice number and immediately save the document so the next time this empty form is opened it will start from the new number and add + and so on. It works great.
At the end of the form there is a "Save" button which automatically save the completed forms in the same folder as the master empty document but using the new invoice number in O2 as a file name. Then automatically close the document so, the user will have to reopen a new empty form to make a new invoice. My code for this button is :
Sub SaveAsO2()
InvoiceNo = Range("O2").Value
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & Application.PathSeparator & "FL" & InvoiceNo & ".xlsm"
ActiveWorkbook.Close
End Sub
Everything works really great except for one thing. This newly saved invoice will have is invoice number in O2 increase by 1 too if we open it in the futur since it keep all the macro active.
So what I want is adding a line to my Save button which delete the code in ThisWorkbook before saving it.
Is it something possible? I'm open to any other way I can achieve what I want if you have a suggestion.
Thanks
Alex