Autoincrement an Alphanumeric value in VBA

StacGuesses

New Member
Joined
Mar 27, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm trying to make a macro to autoincrement my invoice number which is a combination of letters and numbers. I'd like the last number to increase by 1 every time I run the macro.

ex. ABC-2023-01
increasing to
ABC-2023-02

1) do I need to first identify my variable as a string? or is there another data type to use?
2) if I identify it as a string do I then convert to an integer or long data type?

Current code that doesn't work

Sub CreateNewInvoice()

Dim invno As String

invno = Range("C3")

Range("B9, B19:I18").ClearContents

MsgBox "Your next invoice number is" & invno + 1

Range("B9").Select

ThisWorkbook.Save
End Sub



Thanks to all those that can help.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
How about something like:
VBA Code:
Sub CreateNewInvoice()
'
    Dim invno                   As String
'
    invno = Range("C3")
'
    Range("B9, B19:I18").ClearContents
'
    MsgBox "Your next invoice number is" & Left(invno, InStrRev(invno, "-")) & "0" & Mid(invno, InStrRev(invno, "-") + 1) + 1
'
    Application.Goto Range("B9")
'
    ThisWorkbook.Save
End Sub
 
Upvote 0
VBA Code:
Sub CreateNewInvoice()

Dim invno As String, s As Variant

invno = Range("C3")
s = Split(invno, "-")
s(2) = s(2) + 1
invno = s(0) & "-" & s(1) & "-" & Format(s(2), "00")

Range("B9, B19:I18").ClearContents

MsgBox "Your next invoice number is " & invno

Range("B9").Select

ThisWorkbook.Save
End Sub
 
Upvote 0
@johnnyL I'm concerned this will run into issues as the invoice number grows >09.

No worries. I wasn't sure if you always wanted the leading zero or not. Easily fixed.

VBA Code:
Sub CreateNewInvoice()
'
    Dim invno   As String
'
    invno = Range("C3")
    Range("B9, B19:I18").ClearContents
    MsgBox "Your next invoice number is" & Left(invno, InStrRev(invno, "-")) & Format(Mid(invno, InStrRev(invno, "-") + 1) + 1, "00")
'
    Application.Goto Range("B9")
'
    ThisWorkbook.Save
End Sub
 
Upvote 0
ahh Cool.

Thank you.

I'm now struggling with a second challenge.

I have a second macro to saveAs xmlx and the code seem to work fine, but when I go to open the newly save file it is in an unrecognized formal.

Here is the code, and I've attached what the saved file looks like.
 
Upvote 0
ahh Cool.

Thank you.

I'm now struggling with a second challenge.

I have a second macro to saveAs xmlx and the code seem to work fine, but when I go to open the newly save file it is in an unrecognized formal.

Here is the code, and I've attached what the saved file looks like.

@johnnyL

Sub SaveInvoiceExcel()

Dim invTx As String
Dim cliname As String
Dim total As Currency
Dim amt As Currency
Dim GST As Currency
Dim dt_issue As Date
Dim Term As Byte
Dim desc As String
Dim commnt As String
Dim path As String
Dim Fname As String

invTx = Range("C3")
cliname = Range("B9")
total = Range("I38")
amt = Range("I36")
GST = Range("I37")
desc = Range("B18")
commnt = Range("B35")
dt_issue = Range("C4")
Term = Range("C5")
path = "C:\Golden Planet Mining\z_Personal\Stacked Geoscience\Invoices\"
Fname = invTx & " - " & cliname

'copy inv to the invoice sheet to the new worksheet
Sheet2.Copy

'delete all buttons on worksheet
Dim shp As Shape

For Each shp In ActiveSheet.Shapes
shp.Delete
Next shp

'save the new workbook to specified folder
With ActiveWorkbook
.Sheets(1).Name = "Invoice"
.SaveAs filename:=path & Fname, FileFormat:=51
.Close
End With
'add details of invoice to the Invoice Tracking
End Sub
 

Attachments

  • Screenshot 2023-03-28 091014.jpg
    Screenshot 2023-03-28 091014.jpg
    7.7 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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