VBA code to generate Auto Serial Number and Date in excel

Lukma

Board Regular
Joined
Feb 12, 2020
Messages
240
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi Guys

Can anyone help out with a VBA Code that will generate Automatically Serial Number Starting from Column B2 and date in column C2 once name is entered in Column D2

Need a solution for this
below is the VBA code i use but still i cant get around

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("D:D")) Is Nothing Then
With Target(1, 0)
.Value = Date & " " & Time
.EntireColumn.AutoFit
End With
i = 2 '2 indicates from where the date will appear
While Cells(i, 2).Value <> ""
Cells(i, 1).Value = Cells(i, 1).Row - 1 '-1 is connected with i=2
i = i + 1
Wend
End If
Else
Range("D1").Value = Range("D1").Value
End If
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
1608796878132.png
 
Upvote 0
Test this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("D:D")) Is Nothing Then
With Target(1, 0)
.Value = Date & " " & Time
.EntireColumn.AutoFit
End With
With Target(1, -1)
.Value = Target(1, 0).Value
.NumberFormat = "#.00"
.EntireColumn.AutoFit
End With
i = 2 '2 indicates from where the date will appear
While Cells(i, 2).Value <> ""
Cells(i, 1).Value = Cells(i, 1).Row - 2 '-1 is connected with i=2
Cells(i, 1).Value = Cells(i, 1).Row - 1
i = i + 1
Wend
End If
Else
Range("D1").Value = Range("D1").Value
End If
End Sub
if serial number means Number of Records change second with
VBA Code:
With Target(1, -1)
.Value = Target.Row - Range("D7").Row
.NumberFormat = "#"
.EntireColumn.AutoFit
End With
 
Last edited:
Upvote 0
Test this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("D:D")) Is Nothing Then
With Target(1, 0)
.Value = Date & " " & Time
.EntireColumn.AutoFit
End With
With Target(1, -1)
.Value = Target(1, 0).Value
.NumberFormat = "#.00"
.EntireColumn.AutoFit
End With
i = 2 '2 indicates from where the date will appear
While Cells(i, 2).Value <> ""
Cells(i, 1).Value = Cells(i, 1).Row - 2 '-1 is connected with i=2
Cells(i, 1).Value = Cells(i, 1).Row - 1
i = i + 1
Wend
End If
Else
Range("D1").Value = Range("D1").Value
End If
End Sub
if serial number means Number of Records change second with
VBA Code:
With Target(1, -1)
.Value = Target.Row - Range("D7").Row
.NumberFormat = "#"
.EntireColumn.AutoFit
End With
[/QUOTE]
 
Upvote 0
Test this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("D:D")) Is Nothing Then
With Target(1, 0)
.Value = Date & " " & Time
.EntireColumn.AutoFit
End With
With Target(1, -1)
.Value = Target(1, 0).Value
.NumberFormat = "#.00"
.EntireColumn.AutoFit
End With
i = 2 '2 indicates from where the date will appear
While Cells(i, 2).Value <> ""
Cells(i, 1).Value = Cells(i, 1).Row - 2 '-1 is connected with i=2
Cells(i, 1).Value = Cells(i, 1).Row - 1
i = i + 1
Wend
End If
Else
Range("D1").Value = Range("D1").Value
End If
End Sub
if serial number means Number of Records change second with
VBA Code:
With Target(1, -1)
.Value = Target.Row - Range("D7").Row
.NumberFormat = "#"
.EntireColumn.AutoFit
End With
Thanks
Test this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("D:D")) Is Nothing Then
With Target(1, 0)
.Value = Date & " " & Time
.EntireColumn.AutoFit
End With
With Target(1, -1)
.Value = Target(1, 0).Value
.NumberFormat = "#.00"
.EntireColumn.AutoFit
End With
i = 2 '2 indicates from where the date will appear
While Cells(i, 2).Value <> ""
Cells(i, 1).Value = Cells(i, 1).Row - 2 '-1 is connected with i=2
Cells(i, 1).Value = Cells(i, 1).Row - 1
i = i + 1
Wend
End If
Else
Range("D1").Value = Range("D1").Value
End If
End Sub
if serial number means Number of Records change second with
VBA Code:
With Target(1, -1)
.Value = Target.Row - Range("D7").Row
.NumberFormat = "#"
.EntireColumn.AutoFit
End With
Thanks A lot this is just what i need it work perfectly i have been battling with it over few days but now am it make me relieved so glad
 
Upvote 0
you're welcome & thanks for feedback.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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