Serial Number based on date

Eksistenze

New Member
Joined
Feb 17, 2021
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hello,

I am trying to generate a serial number for some entries based on a date that is entered in column H. I have done this using the formulas below:

(This is in my DATA sheet in column E where I just have sequential numbers in column D)
=YEAR('2021 CSEC DISCREPANCY LIST'!H2)&"-"&TEXT(INDIRECT("D"&(2+COUNTIF('2021 CSEC DISCREPANCY LIST'!A$1:A1,YEAR('2021 CSEC DISCREPANCY LIST'!H2)&"-"&"*"))),"000")

(Where the actual serial number goes in the main sheet)
=IF(COUNTA(B2:L2)=0,"ZZZ",'DATA (DO NOT TOUCH)'!E$2)

The problem comes when the entries are sorted, it creates a circular reference and breaks the formula.

I am trying to come up with a VBA solution that generates the serial numbers based on the year in column H so that each time a new entry is added, it ups the serial number by one.

Example:
2020-001
2020-002
2021-001
2020-003
2021-002

Thank you!
 

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,

Check below code:

I have used year in column A and showing serial number in column B.

VBA Code:
Sub showSeries()

Dim lastRow As Integer

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

For rowno = 2 To lastRow
    ActiveSheet.Cells(rowno, "B") = ActiveSheet.Cells(rowno, "A") & "-001"
    For revcheck = rowno - 1 To 2 Step -1
        If ActiveSheet.Cells(rowno, "A") = ActiveSheet.Cells(revcheck, "A") Then
            ActiveSheet.Cells(rowno, "B") = ActiveSheet.Cells(rowno, "A") & "-00" & (CInt(Right(ActiveSheet.Cells(revcheck, "B"), 3)) + 1)
            Exit For
        End If
    Next
Next
   
End Sub

SerialNosBasedOnYear.xlsm
ABCD
1YearS.No.
220212021-001
320212021-002
420222022-001
520212021-003
620212021-004
720202020-001
820192019-001
920192019-002
1020212021-005
1120212021-006
1220212021-007
1320222022-002
14
15
16
data
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,159
Members
448,948
Latest member
spamiki

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