Search code examples
excelvbauserform

Can I run a sub in a VBA userform if something is triple clicked? (or more clicks)


I was wondering if I can run a sub/macro when something within my userform is tripleclicked(or more clicks)? Whereas "something" could be a label, image, textbox or whatever.

Basically using this method:

Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
End Sub

But for a triple click?

I had no luck when searching the web for such information.


Solution

  • But it is intended to be used as a easter egg for someone who clicks on something more times than they are supposed to do. – Nick 1 hour ago

    Interesting :) For this you will have to use _Click(), _DblClick() and a Application.OnTime.

    LOGIC:

    1. Start timer in click event _Click(), _DblClick() if timer is not ON using Application.OnTime.
    2. Automatically stop timer after 1 second.
    3. Check for 3 clicks within 1 second.

    CODE:

    Is this what you are trying?

    Paste this in the userform

    Option Explicit
    
    Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub UserForm_Click()
        ClicksCount
    End Sub
    
    Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        ClicksCount
    End Sub
    
    Private Sub ClicksCount()
        If TimerOn = False Then StartTimer
    
        '~~> Check for the first click
        If StartTick = 0 Then
            StartTick = GetTickCount
            ClickCount = ClickCount + 1
        Else
            EndTick = GetTickCount
            ClickCount = ClickCount + 1
        End If
    
        If ClickCount > 2 Then
            MsgBox "Triple clicked"
            StartTick = 0: EndTick = 0: ClickCount = 0
        End If
    End Sub
    

    Paste this in a module

    Option Explicit
    
    Public StartTick As Long, EndTick As Long, ClickCount As Long
    Public TimerOn As Boolean
        
    Sub StartTimer()
        StartTick = 0: EndTick = 0: ClickCount = 0: TimerOn = True
        Application.OnTime Now + TimeValue("0:00:01"), "StopTimer"
    End Sub
    
    Sub StopTimer()
        StartTick = 0: EndTick = 0: ClickCount = 0: TimerOn = False
    End Sub