Generating auto year in VB

RedMisfit

New Member
Joined
Feb 12, 2019
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi

I am creating a VB form, which updates a spreadsheet, and as part of this creates a unique reference number.

I want the Ref number to be CUST/YY/unique number

I am struggling to get the year automatically, I have tried a couple of ways but it didnt work. Currently I am using the below fix, but would like the 19 to be auto generated so it will change every year. I also need to to start from 1 again when the year changes

eg if the ref was CUST/19/1234 I would like it to go to CUST/20/1 in 2020 and not just clock over to 1235. Thanks

'''defines what the variable is to refer to. _
(The row number of the last used cell in column A.)
emptyROW = Cells(Rows.Count, "A").End(xlUp).Row
lastROW = CLng(Cells(Rows.Count, "A").End(xlUp).Row) - 1

currentURN = "CUST/19" & "/" & CStr(emptyROW)

'Determine emptyRow
emptyROW = WorksheetFunction.CountA(Range("A:A")) + 1

Any assistance will be greatly appreciated

Thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi
You could try with currentURN = "CUST/" & Mid(Year(Now), 3, 2) & "/" & CStr(emptyROW)
Cheers
Sergio
 
Upvote 0
This subroutine will look at the last entry and either add 1 to the last portion, or increse the year by 1 and start back at 1. I wrote it to put the calculated next URN in column A, modify as you need. You could easily turn it into a function to return the calculated URN instead.

Code:
Sub nextURN()


lastRow = CLng(Cells(Rows.Count, "A").End(xlUp).Row)

currentYear = Mid(Cells(lastRow, "A"), 6, 2)

currentCount = Mid(Cells(lastRow, "A"), 9)


If Format(Date, "yy") <> currentYear Then
currentYear = Format(Date, "yy")
currentCount = 0
End If

nextURN = "CUST/" + currentYear + "/" + Format((currentCount + 1))
Cells(lastRow + 1, "A").Value = nextURN

End Sub
Regards
John
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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