Excel VBA Cell format - Increment number format

oO P2K Oo

New Member
Joined
Aug 12, 2018
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Firstly, thank you for your time helping me with my query.

I used Bill Jelen's "Next invoice number" VBA code to help set me up with an invoice template that will generate a new blank invoice and add 1 to the invoice number. My issue is that I have set the first invoice to "SI-00001", but when I use the code below the invoice changes the next invoice number to "SI-2". I would love it if someone could help me have the next invoice number ("SI-00002") and following invoice numbers to keep the 5 digit format, especially for for the tenth/ hundredth/ thousandth invoice number ("SI-00010"/ "SI-00100"/ "SI-01000").

The VBA code I use:

Sub SaveInvoiceBothWaysAndClear()
Dim NewFN As Variant
' Create the PDF First
NewFN = "E:\OneDrive\Invoices\Sales\PDF\Invoice " & Left(Range("K14").Value, 3) & Mid(Range("K14").Value, 4, 5) & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NewFN, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
' Next, Save the Excel File
ActiveSheet.Copy
NewFN = "E:\OneDrive\Invoices\Sales\Excel\Invoice " & Left(Range("K14").Value, 3) & Mid(Range("K14").Value, 4, 5) & ".xlsx"
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
' Increment the invoice number
Range("K14").Value = Left(Range("K14").Value, 3) & 1 + Mid(Range("K14").Value, 4, 5)
' Clear out the invoice fields
Range("A21:B38").ClearContents
End Sub

Many thanks in advance.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Code:
[COLOR=#333333]Range("K14").Value = Left(Range("K14").Value, 3) & [B]Format([/B]1 + Mid(Range("K14").Value, 4, 5)[B], "00000")[/B][/COLOR]
 
Upvote 0
Thanks for that, I put it after the "' Increment the invoice number" code.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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