Consulting

Results 1 to 6 of 6

Thread: VBA Before Double-Click Event executing when not desirable

  1. #1

    Post VBA Before Double-Click Event executing when not desirable

    Hi,

    First time posting to this forum. I hope this is an easy solution!!!

    I am using the Marlett checkbox trick to make a tick appear and disappear on double-click across two ranges (myChecks and ckBoxes). This part works flawlessly currently. However the code is also executing when double-clicking happens almost anywhere else. I have checked these other columns are not in either of the two named ranges. I also have a Change Event written for the Ckboxes range which clears content from some cells in the range depending on what cell was double-clicked. I have noticed that the event is not triggered when double-clicking a merged cell. I take it that 'target' means the active cell so thought that the Intersect code would prevent the event being triggered when the active cell is not in my two ranges however it does still. I could code again for all the other places that a user could be allowed to double-click (i.e. for editing a cell) but it seems like overkill. Please help with an easier solution!!

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    ActiveSheet.Unprotect "***"
        If (Target.Count > 1 And Intersect(Target, Range("myChecks")) Is Nothing) Then Exit Sub
        
        Target.Font.Name = "marlett"
        If Target.Value <> "a" Then
            Target.Value = "a"
            Cancel = True
            ActiveSheet.Protect "***"
        Exit Sub
        End If
        
        If Target.Value = "a" Then
            Target.ClearContents
            Cancel = True
            ActiveSheet.Protect "***"
            Exit Sub
            End If
        
        If (Target.Count > 1 And Intersect(Target, Range("Ckboxes")) Is Nothing) Then Exit Sub
        
        Target.Font.Name = "marlett"
        If Target.Value <> "a" Then
            Target.Value = "a"
            Cancel = True
            ActiveSheet.Protect "***"
            Exit Sub
        End If
        If Target.Value = "a" Then
            Target.ClearContents
            Cancel = True
            ActiveSheet.Protect "***"
            Exit Sub
        End If
        
        ActiveSheet.Protect "***"
    End Sub
    Last edited by papaeight; 03-12-2018 at 03:16 AM. Reason: Updated conditions that trigger the VBA event to run

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum.

    perhaps:
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
        ActiveSheet.Unprotect "***"
        
        If Target.Count > 1 Then Exit Sub
        
        If Intersect(Target, Range("myChecks")) Is Nothing And Intersect(Target, Range("Ckboxes")) Is Nothing Then Exit Sub
        
        With Target
            .Font.Name = "marlett"
            If .Value <> "a" Then
                Cancel = True
                .Value = "a"
                ActiveSheet.Protect "***"
                Exit Sub
            End If
        
            If .Value = "a" Then
                Cancel = True
                .ClearContents
                ActiveSheet.Protect "***"
                Exit Sub
            End If
        End With
        
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    >Target.Count > 1


    It's always false?

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    If (Target.Count > 1 OR Intersect(Target,
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Mancubus. You're a star! Works exactly as required. Of course I see now that my original 'And' conditions were allowing the two If to always return False so the subsequent code would implement both when desired and when not desired. Also I see you making the same point Mana. Thanks SamT that would work too as an OR clause.

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.
    thanks.

    in events, i prefer conditions be written on different statements (to prevent confusion) and end the code immediately, if any of the conditions are not met, rather than resuming it if one condition is met.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Tags for this Thread

Posting Permissions

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