Trapping Left Mouse Click Event Without Using Subclassing.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,621
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

Is there a simple way to intercept the left mouse click event on a worksheet without using Subclassing as opposed to my second post below?

http://www.mrexcel.com/board2/viewtopic.php?t=82011&highlight=subc*


In other words, is there a straightforward Mouse related API or a combination of APIs that could achieve this ? I have been experimenting with other Mouse APIs but with no luck.

Basically,what I want to achieve is to be able to mark a cell with an "X"just by left clicking it with the mouse.This isn't possible as there is no such left click event built-in in Excel.

The Selection_Change is not the answer as this fires when using the Keyboard as well.


Any insight on this would be much appreciated. (y)


Regards.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Re: Trapping Left Mouse Click Event Without Using Subclassin

Chip Pearson's site has a list of Excel Events. I took a look through the text and didn't see any left click events, but I did see right click events? I'm not sure if that will help or not, but you might be able to setup your macro with the right click instead of the left click.


http://www.cpearson.com/excel/events.htm
 
Upvote 0
Re: Trapping Left Mouse Click Event Without Using Subclassin

Two questions:

(1)
Is there a specific range on a sheet, or specific sheets in a workbook, where you want this ability, and other ranges or sheets where you don't?

(2)
Wherever you want the left click event, would you ever want or need the right click event in that same range? In other words, can you live with, for example, a left click in column A, without the ability for a right click event in column A, but then as soon as you move to column B you have the standard right click menu restored?

The idea would be to swap the meaning of the left and right mouse buttons with an API function like

Declare Function SwapMouseButton Lib "user32" _
(ByVal swap As Integer) As Integer

Then place the code for an X in the target cell, siuch as

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If ActiveCell.Value = "x" Then
ActiveCell.Value = ""
Else
ActiveCell.Value = "x"
End If
Cancel = True
End Sub

Call the SwapMouseButton as True when you want (as when a cell in column A is selected with the Selection event), and False when the workbook is closed, deactivated, or you move outside column A.

What all this boils down to is, there is no left mouse click (at least not that I'm aware of), so some creative thought is required for a workaround. Keep in mind, with the API function per above, the mouse would behave that way for all Windows apps until you set it to false, so a word to the wise.
 
Upvote 0
Re: Trapping Left Mouse Click Event Without Using Subclassin

:biggrin:

Nice workaround Tom !

I have never heard of this handy and simple API function but I knew that given the huge number of API functions that are available there just had to be a simple way of solving this problem without having to go through the lengthy process of subclassing.

Great stuff (y) . Thanks very much Cbrine and Tom.
 
Upvote 0
Re: Trapping Left Mouse Click Event Without Using Subclassin

Tom Urtis said:
Two questions:

(1)
Is there a specific range on a sheet, or specific sheets in a workbook, where you want this ability, and other ranges or sheets where you don't?

(2)
Wherever you want the left click event, would you ever want or need the right click event in that same range? In other words, can you live with, for example, a left click in column A, without the ability for a right click event in column A, but then as soon as you move to column B you have the standard right click menu restored?

The idea would be to swap the meaning of the left and right mouse buttons with an API function like

Declare Function SwapMouseButton Lib "user32" _
(ByVal swap As Integer) As Integer

Then place the code for an X in the target cell, siuch as

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If ActiveCell.Value = "x" Then
ActiveCell.Value = ""
Else
ActiveCell.Value = "x"
End If
Cancel = True
End Sub

Call the SwapMouseButton as True when you want (as when a cell in column A is selected with the Selection event), and False when the workbook is closed, deactivated, or you move outside column A.

What all this boils down to is, there is no left mouse click (at least not that I'm aware of), so some creative thought is required for a workaround. Keep in mind, with the API function per above, the mouse would behave that way for all Windows apps until you set it to false, so a word to the wise.

Neat bit of coding slieght of hand Tom.
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,281
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top