+ Reply to Thread
Results 1 to 4 of 4

Fix change event code to add todays date when data is added to multiple cells in column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Fix change event code to add todays date when data is added to multiple cells in column

    I have some Change event code which among other things adds todays date into D when data is first entered into G.
    The issue I'm having is when I paste data into more than one G cell at the same time it doesn't work. How can I fix this?

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Intersect(Target, Range("A6:K" & Cells(Rows.Count, "A").End(xlUp).Row)) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
    
    Application.EnableEvents = False
        Target.WrapText = True
        Target.Rows.AutoFit
    Application.EnableEvents = True
    
    
        If Target.Cells.Count > 1 Then Exit Sub
    
        Application.EnableEvents = False
    
        If Not Intersect(Target, Columns("G")) Is Nothing Then
            If Not IsEmpty(Target) Then
                Target.Offset(, -3).Value = Date
            Else
                Target.Offset(, -3).ClearContents
            End If
        End If
    
        Application.EnableEvents = True
        
    
    On Error Resume Next
    If Intersect(Target, Range("G6:G1048576")) Is Nothing _
    Or Target.Cells.Count > 1 _
    Or Target.Value = vbNullString Then Exit Sub
    
    If Target.Offset(0, -1).Value = vbNullString Then
        Beep
        NewItem.Show
    
    End If
    
        If Target.Cells.Count > 1 Then Exit Sub
         
        If Not Intersect(Target, Range("G2:G" & Cells(Rows.Count, "D").End(xlUp).Row)) Is Nothing Then
            If Target.Value = vbNullString Then
                Range("D" & Target.Row) = vbNullString
            End If
        End If
    
    End Sub

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,657

    Re: Fix change event code to add todays date when data is added to multiple cells in colum

    Here:
    Target.Cells.Count > 1
    you force the procedure to quit if there is more than 1 changed cell. change it to:
    If Intersect(Target, Range("A6:K" & Cells(Rows.Count, "A").End(xlUp).Row)) Is Nothing then Exit Sub
    few lines later it is again (useless, because if the condition were met it would never be executed) so delete:

        If Target.Cells.Count > 1 Then Exit Sub
    and ... I am reading with further on several times checking again.

    Ok. I'll try to suggest complete final version (no attachment, so not tested)



    Private Sub Worksheet_Change(ByVal Target As Range)
    dim rng as Range
    If Intersect(Target, Range("A6:K" & Cells(Rows.Count, "A").End(xlUp).Row)) Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    Target.WrapText = True
    Target.Rows.AutoFit
    If Not Intersect(Target, Columns("G")) Is Nothing Then
      for each rng in Intersect(Target, Columns("G")) 
            If Not IsEmpty(rng) Then
                rng.Offset(, -3).Value = Date
            Else
                rng.Offset(, -3).ClearContents
            End If
      next rng
    End If
    Application.EnableEvents = True
    
    On Error Resume Next
    If not Intersect(Target, Range("G6:G1048576")) Is Nothing then
      for each rng in Intersect(Target, Range("G6:G1048576"))
        if rng.Value <> vbNullString and rng.Offset(0, -1).Value = vbNullString Then
            Beep
            NewItem.Show
        End If
      next rng
    end if
    
    If Not Intersect(Target, Range("G2:G" & Cells(Rows.Count, "D").End(xlUp).Row)) Is Nothing Then
      for each rng in Intersect(Target, Range("G2:G" & Cells(Rows.Count, "D").End(xlUp).Row)) 
          If rng.Value = vbNullString Then
             Application.EnableEvents = False
             Range("D" & rng.Row) = vbNullString
             Application.EnableEvents = True
          End If
      next rng
    End If
    
    End Sub
    I wrote it without testing, so check syntax, etc.

    Summarizing: As I said - remove all checkin of target.cells.count
    In all cases where action on target shall be made cell by cell just do it - for each cell in target (or subset of target) do something.

  3. #3
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Fix change event code to add todays date when data is added to multiple cells in colum

    Thank you Kaper that fixed it.
    Im not very experienced when it comes to VB, is there something further that your recommending I do to fix up the code?

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,657

    Re: Fix change event code to add todays date when data is added to multiple cells in colum

    Hi,
    I am glad it hepled.
    I do not really see the point to react on any changes out of column G (the three special cases are all about some change in column G). So may be you could narrow the field here:
    If Intersect(Target, Range("A6:K" & Cells(Rows.Count, "A").End(xlUp).Row)) Is Nothing Then Exit Sub
    for instance to
    If Intersect(Target, Range("G6:G" & Cells(Rows.Count, "A").End(xlUp).Row)) Is Nothing Then Exit Sub
    Once you do it (or even if not) here
    If Not Intersect(Target, Range("G2:G" & Cells(Rows.Count, "D").End(xlUp).Row)) Is Nothing Then
    you start from G2. If the changed range is simply G2 this part will never be executed, because of the first checking whether target intersects with G6:G_last_

    So it is probably right to write:
    If Not Intersect(Target, Range("G6:G" & Cells(Rows.Count, "D").End(xlUp).Row)) Is Nothing Then
    if you do not expect that
    Cells(Rows.Count, "D").End(xlUp).Row
    is different (lower row number) than
    Cells(Rows.Count, "A").End(xlUp).Row
    which is checked just at the beginning you could change also this part.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 12-10-2012, 06:57 AM
  2. Replies: 5
    Last Post: 09-23-2012, 08:51 AM
  3. VBA Code: Change event, multiple if's for a range
    By dhopman in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-13-2012, 11:27 AM
  4. Change event - Inputing date into specific column
    By everettjsj2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2010, 12:36 PM
  5. [SOLVED] change event for multiple cells
    By Guy Normandeau in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-20-2006, 04:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1