Edit macro to perform a Save Workbook & Next Invoice Number

gatorbug

New Member
Joined
Aug 16, 2018
Messages
5
I'm new to VBA coding and have an issue with my SaveInvWithNewName code. My workbook contains one open worksheet (Invoice) and four hidden worksheets (Service Date, Work Order, Purchase Order, & Employee). The hidden worksheets need to remain hidden. The open worksheet pulls data from the four hidden worksheets.

I've added a shape and assigned macro to the open worksheet. When I click the shape to perform “Save & Next Invoice Number” function, it goes to the next invoice number and only saves the open worksheet; it does not save the hidden worksheets. Is there a code that can perform a “Save Entire Workbook & Next Invoice Number”?

VBA code:

Sub SaveInvWithNewName()
Dim NewFN As Variant
' Copy Invoice to a new workbook
ActiveWindow.SelectedSheets.Copy
NewFN = "C:\ABC\Invoices\" & Range("D7").Value & ".xlsx"
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
NextInvoice
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This should do it

VBA Code:
Sub SaveInvWithNewName()

    Application.ScreenUpdating = False    

    Dim NewFN As String
  
    NewFN = "C:\ABC\Invoices\" & Range("D7").Value & ".xlsx"
  
    ThisWorkbook.Worksheets.Copy '// THIS WILL COPY ALL THE SHEETS
  
    With ActiveWorkbook
        .SaveAs _
            Filename:=NewFN, _
            FileFormat:=xlOpenXMLWorkbook
      
        .Close _
            SaveChanges:=False
    End With

    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
Sub SaveInvWithNewName() Application.ScreenUpdating = False Dim NewFN As String NewFN = "C:\ABC\Invoices\" & Range("D7").Value & ".xlsx" ThisWorkbook.Worksheets.Copy '// THIS WILL COPY ALL THE SHEETS With ActiveWorkbook .SaveAs _ Filename:=NewFN, _ FileFormat:=xlOpenXMLWorkbook .Close _ SaveChanges:=False End With Application.ScreenUpdating = True End Sub
Thank you. Code is very helpful and it does save entire workbook; however, it does not change Range ("D7") to the next invoice number after clicking "Save & Next Invoice Number" assigned macro button.

First invoice I created is invoice 20-1. When I click "Save & Next Invoice Number", it does save as filename as 20-1, but it does not change Range ("D7") to next invoice number 20-2. Therefore, if I click "Save & Next Invoice Number" again, a message box opens saying ( A file name 'C:\ABC\Invoices\20-1.xlsx' already exists in this location. Do you want to replace it? )

Suggestion?
 
Upvote 0
Sorry about that. I've added that in for you now.

VBA Code:
Sub SaveInvWithNewName()

    Application.ScreenUpdating = False

    Dim NewFN As String
    Dim CurrentInvoiceNumber As String
    Dim NextInvoiceNumber As String
    
    CurrentInvoiceNumber = Range("D7").Value
    NextInvoiceNumber = "20-" & CStr(Split(CurrentInvoiceNumber, "-")(1) + 1)
    NewFN = "C:\ABC\Invoices\" & CurrentInvoiceNumber & ".xlsx"
    
    Debug.Print CurrentInvoiceNumber, NextInvoiceNumber
  
    ThisWorkbook.Worksheets.Copy '// THIS WILL COPY ALL THE SHEETS

    With ActiveWorkbook
        .SaveAs _
            Filename:=NewFN, _
            FileFormat:=xlOpenXMLWorkbook

        .Close _
            SaveChanges:=False
    End With

    Range("D7").Value = NextInvoiceNumber
    Application.ScreenUpdating = True

End Sub

Just wondering, is the invoicing based on the year i.e 20-1 for the 1st invoice of 2020 and so on.

If so there'll be a little bit more code to add to get that to swap over for a new year.
 
Upvote 0
Perfect! This works great. Yes, 20 does represent the year. For example, when I start a new invoice for 2021, it will start with 21-1 (21 represents year and 1 will be the first invoice for that year). Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top