Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,806
- Office Version
- 2016
- Platform
- Windows
Hi all.
AS we know, the Worksheet_SelectionChange event fires when a cell is selected either with the mouse or with the keyboard.
I am trying to come up with an event that only fires when the cell is clicked with the mouse. So far, I 've only managed to do this :
This works fine except for 2 limitations that I can't seem to work around namely :
1- Double-clicking the cell also fires the event which I don't want.
2- The event doesn't fire if the cursor is already in the cell, and one clicks that cell.
I guess I could run the PeekMessage API within a loop/Timer (specially to overcome the second limitation) but that would tie up the application too much and/or prevent other codes from running.
Any other ideas ?
AS we know, the Worksheet_SelectionChange event fires when a cell is selected either with the mouse or with the keyboard.
I am trying to come up with an event that only fires when the cell is clicked with the mouse. So far, I 've only managed to do this :
Code:
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function PeekMessage Lib "user32" _
Alias "PeekMessageA" _
(lpMsg As MSG, _
ByVal hwnd As Long, _
ByVal wMsgFilterMin As Long, _
ByVal wMsgFilterMax As Long, _
ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" _
() As Long
Private Const PM_NOREMOVE = &H0
Private Const PM_NOYIELD = &H2
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tMSG As MSG
WaitMessage
Call PeekMessage(tMSG, 0, 0, 0, PM_NOREMOVE + PM_NOYIELD)
If tMSG.message = WM_MOUSEMOVE And tMSG.wParam = 0 Then
MsgBox "You Clicked :" & Target.Address
End If
End Sub
1- Double-clicking the cell also fires the event which I don't want.
2- The event doesn't fire if the cursor is already in the cell, and one clicks that cell.
I guess I could run the PeekMessage API within a loop/Timer (specially to overcome the second limitation) but that would tie up the application too much and/or prevent other codes from running.
Any other ideas ?