Selected Answer
Papo
This will work if your tracking numbers are stored sequentially in column A of the sheet where your button is (like my file attached) AND the last entry is the last number allocated. Click the green button (currently in column A) and new numbers will be added.
It works by finding the last used row. The If test (in bold below) compares the last two digits of the current year (from Year(Now()), i.e. the year of todays date ), say 21, with the first two digits of the last used cell in A. If it's the first row (so no numbers yet), it makes n become 1. Likewise if early next rear (or later years) it compares "21" from the last cell with 22 for the new year.
Otherwise, it adds 1 to n (the last 3 digits of the last tracking number).
Then it sets the next cell down to the two year digits, plus a "-" and the incremented number.
Private Sub CommandButton1_Click()
Dim r As Integer, n As Integer
r = Range("A" & Rows.Count).End(xlUp).Row 'find last used row
If Right(Year(Date), 2) <> Left(Range("A" & r), 2) Then 'if no tracking number yet or year changed
n = 1
Else 'normally take last 3 digits of last and add 1
n = Right(Range("A" & r).Value, 3) + 1
End If
'set next cell to desired format
Range("A" & r + 1) = Right(Year(Now()), 2) & "-" & Format(n, "00#")
End Sub
You can't check that the year end bit works as it stands but you could do this check (to simulate a change of year) to see that the trailing number bit resets:
1) Click the button a few times so you have numbers in column A.
2) Change the If statement above to this (in the VB Explorer):
If Right(Year("01/01/2022"), 2) <> Left(Range("A" & r), 2) Then
(which will give the year of that bold 22 date) and if you click the button again, the numbers will restart from 001.
3) Undo that change in VB Explorer and delete the test values in A.
Hope this works for you.