Generate UniqueId when new/multiple rows are added

Vbanoob98

Board Regular
Joined
Sep 13, 2019
Messages
128
Hi all,

I found this code from @Rick Rothstein

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Const CourseColumn As String = "b"
  Const UniqueIDcolumn As String = "AF"
  If Target.Count > 1 Then Exit Sub
  If Not Intersect(Target, Columns(CourseColumn)) Is Nothing Then
    If IsEmpty(Cells(Target.Row, UniqueIDcolumn).Value) Then
      Cells(Target.Row, UniqueIDcolumn).Value = Format(Now, "'mmddyyyy") & Format(100 * Timer, "0000000")
    End If
  End If
End Sub

It works perfectly except it doesnt work when you copy/paste multiple items/rows. Is there a way to do that?

Thanks !
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Ok so I figured it out but I now ran into another problem. How to have 2 different interesect checks?

VBA Code:
Option Explicit

Private Sub Worksheet_Change _
            ( _
                       ByVal Target As Range _
            )

  Const s_CheckColumn As String = "Q:Q" 'Column that will be changing
  Const s_CountColumn As String = "AE:AE" 'Column Counter
  Const s_CountDate As String = "Af:Af" 'date added
    
    
  If Intersect(Target, Range(s_CheckColumn)) Is Nothing Then
   If Intersect(Target, Range(ID_CheckColumn)) Is Nothing Then Exit Sub
  Dim rngCell As Range
  For Each rngCell In Intersect(Target, Range(s_CheckColumn))
    With Range(s_CountColumn).Cells(rngCell.Row)
      .Value2 = IIf(.Value2 <> vbNullString, .Value2 + 1, IIf(rngCell.Value2 <> vbNullString, 0, vbNullString))
   
    End With
    Next rngCell

Const ID_CheckColumn As String = "b:b"
Const ID_CountColumn As String = "Ag:Ag" 'Column Counter

If Intersect(Target, Range(ID_CheckColumn)) Is Nothing Then Exit Sub
    
Dim rngCellID As Range
  For Each rngCellID In Intersect(Target, Range(ID_CheckColumn))
    With Range(ID_CountColumn).Cells(rngCellID.Row)
      .Value = Format(Now, "'mmddyyyy") & Format(100 * Timer, "0000000")
    End With
    Next rngCellID

    
End Sub

Any ideas?
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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