+ Reply to Thread
Results 1 to 6 of 6

SelectionChange event not firing

Hybrid View

  1. #1
    Registered User
    Join Date
    01-30-2019
    Location
    Heythuysen, Netherlands
    MS-Off Ver
    Office 2019
    Posts
    6

    Question SelectionChange event not firing

    Hi all,

    First post on the forum, so 'Hi' to you all! I've used your forum in lots of cases to help me address issues in VBA programming, but now I've encountered one that is quite persistent. I'm on Excel 2019 and am looking to create an audit trail for capturing changes to a planning file I manage at work.

    Capture.JPG

    Above is the audit trail I am looking for. The problem I am having is that the "Previous Value" is not captured due to a Worksheet_SelectionChange not firing. Please find the code below that is present in the target worksheet.

    Dim PreviousValues As Variant
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        'Store current values of all cells
        Application.EnableEvents = True
        PreviousValues = Target.Value
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim LR     As Long
        Dim c      As Range
        
        On Error Resume Next
        
        Application.EnableEvents = False
        For Each c In Target
            If Target(c).Value <> PreviousValues(c) Then
                With Sheets("AuditTrail")
                         LR = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                        .Cells(LR, "A").Value = Now
                        .Cells(LR, "B").Value = Environ("UserName")
                        .Cells(LR, "C").Value = ActiveSheet.Name & "!" & c.Address
                        If PreviousValues(c) = "" Then
                            .Cells(LR, "D").Value = "BLANK!"
                        Else
                            .Cells(LR, "D").Value = PreviousValues(c)
                        End If
                        If c.Value = "" Then
                            .Cells(LR, "E").Value = "BLANK!"
                        Else
                            .Cells(LR, "E").Value = c.Value
                        End If
                End With
            End If
        Next c
    Ending:
        Application.EnableEvents = True
        
    End Sub
    As said previously, the Worksheet_Change event does fire, but the Previous Value is empty as the Worksheet_SelectionChange event does not fire. I've tested and excluded Application.EnableEvents was set to False. Does anyone have any idea where I should be looking?

    Best regards,
    Don

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: SelectionChange event not firing

    I think the event is firing but your variable isn't holding the target value.

    Read this to learn all about variable scope: https://stackoverflow.com/questions/...ariable-in-vba

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: SelectionChange event not firing

    If you change one cell, the PreviousValues will not be an array, and your Change event code won't work. Equally, if you did change more than one value, it will be a 2 dimensional array, not 1D, so your code will fail. You won't know that either way though because you stuck On Error Resume Next at the top of it, so nothing will appear to be wrong.

    Personally, my preference would be to use just the Change event, store the Target.Value in your variable, disable events, then use Application.Undo to remove the changes and then test the old value(s) as appropriate. You can then reinstate the changes and reset events.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    01-30-2019
    Location
    Heythuysen, Netherlands
    MS-Off Ver
    Office 2019
    Posts
    6

    Re: SelectionChange event not firing

    Hi all,

    Thanks for your quick replies. Scoping shouldn't be the issue (as far as I can tell now), as I have declared the PreviousValues outside of the events, so it can be used by both Change and SelectionChange events. I had it working yesterday and it could detect all changes in an array and make note of that in the AuditTrail worksheet, which is a functionality I'd like to preserve as I am often finding myself adjusting multiple cells in the same time (proprietary information, so unable to share). Hoping you can have a look and help me out, thanks again!

    Best regards,
    Don
    Attached Files Attached Files
    Last edited by dfriederichs; 01-30-2019 at 07:43 AM.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: SelectionChange event not firing

    Respectfully, there is no way the code you posted ever worked. Target(c).Value and PreviousValues(c) make no sense at all if c is a Range object, in addition to the errors I mentioned previously.

    You could do something like this:

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim LR     As Long
        Dim x As Long, y As Long
        Dim a As Range
        Dim sel As Range
        Dim PreviousValues As Variant
        
    '    On Error Resume Next
        Application.EnableEvents = False
        Set sel = Selection
        Set a = Target
            PreviousValues = a.Value
            Application.Undo
            If a.Count > 1 Then
            For x = 1 To a.Rows.Count
                For y = 1 To a.Columns.Count
                    If a.Cells(x, y) <> PreviousValues(x, y) Then
                With Sheets("AuditTrail")
                         LR = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                        .Cells(LR, "A").Value = Now
                        .Cells(LR, "B").Value = Environ("UserName")
                        .Cells(LR, "C").Value = ActiveSheet.Name & "!" & a.Cells(x, y).Address
                        If PreviousValues(x, y) = "" Then
                            .Cells(LR, "D").Value = "BLANK!"
                        Else
                            .Cells(LR, "D").Value = PreviousValues(x, y)
                        End If
                        If a.Cells(x, y).Value = "" Then
                            .Cells(LR, "E").Value = "BLANK!"
                        Else
                            .Cells(LR, "E").Value = a.Cells(x, y).Value
                        End If
                End With
            End If
            Next y
        Next x
        Else
                     If a.Value <> PreviousValues Then
                With Sheets("AuditTrail")
                         LR = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                        .Cells(LR, "A").Value = Now
                        .Cells(LR, "B").Value = Environ("UserName")
                        .Cells(LR, "C").Value = ActiveSheet.Name & "!" & a.Address
                        If PreviousValues = "" Then
                            .Cells(LR, "D").Value = "BLANK!"
                        Else
                            .Cells(LR, "D").Value = PreviousValues
                        End If
                        If a.Value = "" Then
                            .Cells(LR, "E").Value = "BLANK!"
                        Else
                            .Cells(LR, "E").Value = a.Value
                        End If
                End With
            End If
           End If
        Target.Value = PreviousValues
        sel.Select
    Ending:
        On Error GoTo 0
        Application.EnableEvents = True
        
    End Sub
    Last edited by romperstomper; 01-30-2019 at 08:09 AM.

  6. #6
    Registered User
    Join Date
    01-30-2019
    Location
    Heythuysen, Netherlands
    MS-Off Ver
    Office 2019
    Posts
    6

    Re: SelectionChange event not firing

    Hi Rorya,

    Thank you for looking into the code! I had to switch the PreviousValues and a.Value columns to have the macro insert in the proper column, but other than that it works like a charm!!!

    Best regards,
    Don

+ 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. SelectionChange() Event Stops Firing
    By jo15765 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-21-2017, 09:25 AM
  2. [SOLVED] Userform multipage control - exit event not firing or event order
    By jane serky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2013, 10:23 AM
  3. How to prevent SelectionChange event firing before Change event?
    By franklyn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2012, 05:17 AM
  4. [SOLVED] VBA + SelectionChange event
    By HuskerBronco in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-13-2011, 06:36 PM
  5. Worksheet SelectionChange Event
    By mjack003 in forum Excel General
    Replies: 2
    Last Post: 05-08-2006, 03:35 PM
  6. SelectionChange event
    By Kate in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-22-2005, 02:10 PM
  7. SelectionChange event
    By Hayeso in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2005, 11:05 AM

Tags for this Thread

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