I'm assuming the problem is accidental clicks on the buttons (as per @Tim Williams comment, hovering itself won't trigger clicks ... unless you maybe have some accessibility assistance switched on that does that for you?).
All of the following code needs adding to the code-behind of the UserForm (ie where you currently have your CommandButton162_Click
Sub). In this example, I am showing code for only 2 buttons ... but you can replicate the relevant _Click and _MouseMove Subs for your 6 buttons. You will also need to either rename them (eg from 'CommandButton1_Click' to 'CommandButton162_Click' and from 'CommandButton1_MouseMove' to 'CommandButton162_MouseMove' etc) or add them yourself using the drop-downs at the top of the code window (which I guess you did for 'CommandButton162_Click') ... either way, this code won't work without using the right names.
I've added comments throughout the code so you can see how it works. The following needs adding at the top of the UserForm, after Option Explicit
and before the first Sub or Function:
Private Const mBUTTON_DELAY As Single = 0.75 ' delay in seconds ... adjust as required
Private mButtonTime As Single ' holds the time that a 'button hover' starts
Private mButtonName As String ' holds the name of the button that is being hovered over
And this needs adding below, with all other Subs and Functions:
' you need a Click event handler for each of your 6 buttons ... here for demo I've added
' only for 2 buttons ... each needs to call HandleClick
Private Sub CommandButton1_Click()
HandleClick
End Sub
Private Sub CommandButton2_Click()
HandleClick
End Sub
' handle a click on any of the 6 buttons ... after testing, you might want to get rid of
' the 'Else' and just have code for when the click is after the delay ... to do something
' different for each of your 6 buttons, you could have a further 'If' statement (see
' commented-out example) ... note it is the button NAME of the button that is stored in
' 'mButtonName' and not the button CAPTION (ie not the text displayed on the button)
Private Sub HandleClick()
If Timer - mButtonTime > mBUTTON_DELAY Then
Debug.Print Now, mButtonName & " clicked after delay ... do your thing!"
' If mButtonName = CommandButton1.Name Then
' ' do something for CommandButton1
' ElseIf mButtonName = CommandButton2.Name Then
' ' do something for CommandButton2
' End If
Else
Debug.Print Now, mButtonName & " clicked during delay ... ignore"
End If
End Sub
' if the mouse is moving over the UserForm (ie not over a button or any other control)
' then reset the stored button name
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
mButtonName = vbNullString
End Sub
' you need a MouseMove event handler for each of your 6 buttons ... here for demo I've
' added only for 2 buttons ... each needs to call HandleMouseMove passing in the button
' itself
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
HandleMouseMove CommandButton1
End Sub
Private Sub CommandButton2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
HandleMouseMove CommandButton2
End Sub
' if the mouse has just started to hover over a button then save its name and the time
' so that if a Click event is received, we can test when the hover started
Private Sub HandleMouseMove(btn As MSForms.CommandButton)
If mButtonName <> btn.Name Then
mButtonName = btn.Name
mButtonTime = Timer
End If
End Sub