VBA code to function differently on right & left click

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,516
Office Version
  1. 2016
Platform
  1. Windows
Dear Friends,

If Possible, a small VBA code is required which I would assign to a shape named "Index"

I want the Range("A1").value = "Right" when the shape is right clicked
I want the Range("A1").value = "Left" when the shape is left clicked

Regards,

Humayun
 
Pls see below pic. There is no option to assign macro

1713278548723.png
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
It should work if you choose View Code.
I think I might have created that other menu using a form control label.
 
Upvote 0
It should work if you choose View Code.
I think I might have created that other menu using a form control label.

The vew code directs me to below

VBA Code:
Private Sub Label1_Click()

End Sub

Then I pasted your provided code in there but nothing happens
 
Upvote 0
In where? Inside that sub? Then no, below. Please show, don't just tell.
 
Upvote 0
No. The operative word being "below". From post 8:
You will end up with a click event sub that you don't need. Paste the code I gave you below that then try it.
 
Upvote 0
No. The operative word being "below". From post 8:

Tried both ways but to no avail :(

Initially I pasted your provided code under the click event without removing it pls see below

VBA Code:
Private Sub Label1_Click()

End Sub

Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'put your own code in this section for what you want to happen with right vs left click
If Button = 1 Then
    Range("A1").Value = "Right"
Else
    Range("A1").Value = "Left"
End If

ActiveCell.Select
End Sub

& then tried after removing the click event

VBA Code:
Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'put your own code in this section for what you want to happen with right vs left click
If Button = 1 Then
    Range("A1").Value = "Right"
Else
    Range("A1").Value = "Left"
End If

ActiveCell.Select
End Sub
 
Upvote 0
Don't know what the issue is. Worked for me. If you want to post a wb copy on a file share maybe I can see what the problem is.
Or first try putting a break point on the first if line, then go back to the sheet and click on the label and see if the code runs. If not, try going through the motions again to assign a macro - this time choose the button event code. If none of that works then I'm out of ideas without looking at your wb.
 
Upvote 0
Don't know what the issue is. Worked for me. If you want to post a wb copy on a file share maybe I can see what the problem is.
Or first try putting a break point on the first if line, then go back to the sheet and click on the label and see if the code runs. If not, try going through the motions again to assign a macro - this time choose the button event code. If none of that works then I'm out of ideas without looking at your wb.

My Bad!
I forgot to exit the design mode that's because I don't work at all with the active X controls
Now the solution you provided is working just perfect

I apologize for the trouble & many thanks for the continuous follow up

Regards,
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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