Generating a purchase order number incorporating month/year

dkmiller16

New Member
Joined
Oct 31, 2013
Messages
13
I am trying to create a form that will generate a PO#, I would like to have the first 4 numbers be the current year/month "yy/mm" followed by a 2 digit incrementing number starting with 01. For example if I create 4 PO#'s in April 2021 and then 3 in May of 2021 I would have the following.

210401
210402
210403
210404
210501
210502
210503

This will allow me to look at the PO# and determine what month/year it was created. For this purpose I will never exceed 99 PO#'s in a single month.

Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this :

VBA Code:
Option Explicit

Sub Test()

Dim i As Long

For i = 1 To 1
    Range("A1").Value = NewID
Next i

End Sub


Public Function NewID() As String

Dim m As String, y As String
Static inclNum As Integer
Static lastMonth As Integer

inclNum = inclNum + 1
m = Format(Date, "mm")
y = Format(Date, "yy")

If lastMonth = 0 Then lastMonth = CInt(m)

If CInt(m) <> lastMonth Then
    lastMonth = CInt(m)
    inclNum = 1
End If

NewID = CStr(inclNum)

If Len(NewID) < 2 Then NewID = "0" & NewID

NewID = y & m & NewID


End Function
 
Upvote 0
Solution
Try this :

VBA Code:
Option Explicit

Sub Test()

Dim i As Long

For i = 1 To 1
    Range("A1").Value = NewID
Next i

End Sub


Public Function NewID() As String

Dim m As String, y As String
Static inclNum As Integer
Static lastMonth As Integer

inclNum = inclNum + 1
m = Format(Date, "mm")
y = Format(Date, "yy")

If lastMonth = 0 Then lastMonth = CInt(m)

If CInt(m) <> lastMonth Then
    lastMonth = CInt(m)
    inclNum = 1
End If

NewID = CStr(inclNum)

If Len(NewID) < 2 Then NewID = "0" & NewID

NewID = y & m & NewID


End Function
@Logit that is exactly what I was looking for. Thank you for that. I have done enough with vba that I am pretty sure I can incorporate that into a form to populate a field. A newb question I have though. Where is it storing the last value/number that it created. No matter if I delete A1 or open a new tab and run the macro, it knows what the next number should be.
 
Upvote 0
Well .. basically it is all contained within the Public Function NewID() As String and inclNum = inclNum + 1

Incrementing the number by 1.

It does not have any code to limit the number to 99 then start over at 01 again.

You'll have to "seed" that number yourself.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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