PDA

View Full Version : Left and Right Mouse Click Event



PcMax
10-24-2011, 11:02 PM
Hallo

In Excel2003 VBA can I catch the mouse before running:

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Left and Right button for a possible use?

Thanks!

https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg (https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg)
https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgxLtKj969oiIu7zNb94AaABAg (https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgxLtKj969oiIu7zNb94AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg)
https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugx12mI-a39T41NaZ8F4AaABAg.9iDQqIP56NV9iFD0AkeeJG (https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugx12mI-a39T41NaZ8F4AaABAg.9iDQqIP56NV9iFD0AkeeJG)
https://www.youtube.com/watch?v=vXyMScSbhk4&lc=Ugxa2VYHMWJWXA6QI294AaABAg.9irLgSdeU3r9itU7zdnW Hw (https://www.youtube.com/watch?v=vXyMScSbhk4&lc=Ugxa2VYHMWJWXA6QI294AaABAg.9irLgSdeU3r9itU7zdnW Hw)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg.9htChVuaX9W9htG01cKBzX (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg.9htChVuaX9W9htG01cKBzX)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg.9ht16tzryC49htJ6TpIO XR (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg.9ht16tzryC49htJ6TpIO XR)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgwMKwGZpDjv7vi7pCx4AaABAg (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgwMKwGZpDjv7vi7pCx4AaABAg)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg.9ht16tzryC49htOKs4jh 3M (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg.9ht16tzryC49htOKs4jh 3M)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgxVW-am20rQ5GFuJ9F4AaABAg (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgxVW-am20rQ5GFuJ9F4AaABAg)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)

Excel Fox
10-24-2011, 11:05 PM
Yes you can using



Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True
MsgBox "Double-Clicked"

End Sub

OR


Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True
MsgBox "Right-Clicked"

End Sub

PcMax
10-24-2011, 11:43 PM
Hallo
Let's analyze:


Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True
MsgBox "Right-Clicked"

End Sub
In a paper without a password by running the code occur at least two possibilities.
Example: I make right click on a cell and activate the macro, but if I were on an image this command does not work.
You can disable the selection image forever without protect the sheet?

Excel Fox
10-24-2011, 11:48 PM
To disable the selection of image, you will have to protect the sheet. If you want users to be able to edit the cells, you can unlock the cells, and then protect the sheet.

PcMax
10-25-2011, 12:18 AM
Hallo

This is an example of code to enable I can not enter password


Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Application.CommandBars.FindControl(ID:=130).Execu te
end sub

I would like to draw a line on the sheet and images

Excel Fox
10-25-2011, 01:41 AM
You can protect the sheet through code and then allow to draw a line through code. To do this, protect the sheet(s) by



Sheet1.Protect UserInterfaceOnly:=True, Password:="xyz"

PcMax
10-25-2011, 06:05 PM
Meanwhile, thank you for the answers, but if I run RightClick on protected sheet over an image the event does not Run.
Here we are again as the first

Excel Fox
10-26-2011, 06:44 AM
Why do you want to right-click? You can use a normal click, and the assigned macro will run even if the sheet is protected. And if you want the shapes also to be unprotected, then use



Sheet1.Protect UserInterfaceOnly:=True, Password:="xyz", DrawingObjects:=False

PcMax
10-27-2011, 06:31 PM
Hallo
Still to solve the problems of selection wonder how the subject should be written the following code:

Sub Test()
ActiveSheet.Shapes("Picture 309").Select 'I select the object
ActiveSheet.Shapes("Picture 309").Deselect 'Here I would like to deselect the object
End Sub
Obviously, the code does not work ...

Excel Fox
10-27-2011, 08:32 PM
Sub Test()
ActiveSheet.Shapes("Picture 309").TopLeftCell.Select
End Sub

EQX
11-24-2012, 04:54 AM
Yes you can using



Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True
MsgBox "Double-Clicked"

End Sub

OR


Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True
MsgBox "Right-Clicked"

End Sub




beg pardon, 1 year later :), am looking for the left click version for events. this would be for a mouse left click only, so can tab into cell without the event taking place. thanks.