How to generating combined date and serial number in excel vba?

Leorand

New Member
Joined
Nov 25, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello everyone, I have question about title above. I try to make a serial number that contain today's date format like this 221222 and serial number start from 01 after the date, and it will change's after the next day. Can anyone help me with this problem?

I've try this code in my file :

num = Application.WorksheetFunction.CountA(Sheet1.Range("C:C"))

If num > 1 Then
Sheet1.Range("A" & num + 2).Value = Date & num - 1
End If

And the result is like this image bellow

1671694077044.png
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Maybe something like this?
VBA Code:
Sub addSerial()
  Dim lRow As Long
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
 
  If Left(Cells(lRow, 1).Value, 6) = Format(Now(), "ddmmyy") Then
    Cells(lRow + 1, 1).Value = Format(Now(), "ddmmyy") & Format(Mid(Cells(lRow, 1).Value, 7, Len(Cells(lRow, 1).Value)) + 1, "00")
  Else
    Cells(lRow + 1, 1).Value = Format(Now(), "ddmmyy") & Format(1, "00")
  End If
End Sub
1671698105953.png
 
Upvote 0
VBA Code:
Sub work_in()
            
            Dim store As String
            Dim k As Integer
            store = Evaluate("text(day(today()),""00"")&text(month(today()),""00"")&text(today(),""yy"")")

                For k = 1 To 10
                        Cells(k, 1) = store & WorksheetFunction.Text(k, "00")
                Next k

End Sub
 

Attachments

  • 1671701031241.png
    1671701031241.png
    63.3 KB · Views: 7
Upvote 0
Solution
Maybe something like this?
VBA Code:
Sub addSerial()
  Dim lRow As Long
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
 
  If Left(Cells(lRow, 1).Value, 6) = Format(Now(), "ddmmyy") Then
    Cells(lRow + 1, 1).Value = Format(Now(), "ddmmyy") & Format(Mid(Cells(lRow, 1).Value, 7, Len(Cells(lRow, 1).Value)) + 1, "00")
  Else
    Cells(lRow + 1, 1).Value = Format(Now(), "ddmmyy") & Format(1, "00")
  End If
End Sub
View attachment 81389
Thankyou for your answer, I will try your code in my excel.
 
Upvote 0
Thankyou for your answer Flashbond, shinigamilight I've got the answer for my code base on your code.
It's help me a lot.
:)
Case Closed.
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,220
Members
448,876
Latest member
Solitario

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