Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How to disable right mouse on sheets at bottom click"

0

Hi

I want to disable the right mouse click on my sheets in a workbook when it loads

I have seached the internet and found a guy who said use this bit of code with excell 2007

He said press alt f11 to bring up the VB page then look on the left hand side and double

click on thisworkbook  then put the code in the right hand page BUT it has a load of code in there like

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Call ClsApp
End Sub

Private Sub Workbook_Open()
    Dim Resp As Long, DaysLeft As Integer, LstRw As Long, Fl

So I do not know where to part to put the code.

This is the code to disable the the right mouse

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.CommandBars("Ply").Enabled = True
End Sub

Private Sub Workbook_Open()
    Application.CommandBars("Ply").Enabled = False
End Sub

So could anyone tell me were to put that cod e so when it runs on load of the workbook

Many Thanks

Kim

Answer
Discuss

Answers

0

Kim

Revised answer 20 December 2023:

Given your problems wiith Willie's solution (which works for me), please find attached a file in which the right click is disabled.

If you open it (with macros enabled), a right mouse click will give the message "Sorry, right click not allowed!" (for demo purposes- you can later comment out the line starting MsgBox).

This code is in the Workbook section of Workbook section of the the file:

Option Explicit

Dim WithEvents App As Application


Private Sub App_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

    Cancel = True
    MsgBox "Sorry, right click not allowed!"

End Sub

Private Sub Workbook_Open()
  Set App = Application
End Sub

This worked for me in both Excel 365 and 2010 (though I had to act on the yellow warning bar under the ribbon stating "Security Warning    Macros have been disabled  <<Enable content>>" (button).

Any file you open at the same time should also have the right mouse click disabled (since it's operation at an application level).

If it doesn't work for you (you don't get the message), check your Trust Center- you may have Macro settings item set to "Disable all macros without notification" (in which case change to "...with notication" or enable all- though that can be risky). Note that you can see macro code in VBA even with macros disabled.

If you make files from this, you can send them to others and they won't be able to use right mouse click PROVIDED macros are enabled (you can force that, see the Tutorials section).

You can also disable right mouse click for yourself for all files by copying the code into the Workbook section of the personal workbook project in VBA , called VBAProject(PERSONAL.XLSB). This however will do nothing to other users-  they need to receive a file including the code above.

Hope that fixes your problem. If so, please remember to mark this Answer as Selected

Discuss

Discussion

Kim, forgot to say, please edit your question to change the title- to something like "How to disable right mouse click"- this will guide others too. Also do not put it in capitals- we don't like being shouted at!
John_Ru (rep: 6142) Dec 20, '23 at 9:06 am
John-RU

Sorry to say that does not work.
If I put my mouse on any input box I have NO RIGHT CLICK

But if I put it on any sheet at bottomI still can get right mouse click

I want it the other way round as I have stated  IE  no right click on sheets, right mouse on input box's

Kim
kimzac (rep: 2) Dec 20, '23 at 1:20 pm
Kim

I think this is the first time you have mentioned InputBoxes- your file doesn't contain any or any clue it might! Are you really referring to InputBoxes on UserForms?

Also, when you say "if I put it on any sheet at bottom, I still can get right mouse...." , are you referring to right clicks on the status bar or horizontal schroll bar?

More fundementally:      

     1) your question said "I have seached the internet and found a guy who said use this bit of code with Excel 2007"- why don't you ask him?    

     2) specifically what are you trying to prevent the user from doing? (There may be other ways).

I won't be doing anything else on this until you clarify your question. I'll suggest Willie does likewise.
John_Ru (rep: 6142) Dec 20, '23 at 3:29 pm
John-Ru
Ok lets get a few things correct .
At no point on my first post OR any post did i say about right mouse clicks in a CELL/input box.
If you look at my very first post I did say

"I want to disable the right mouse click on my sheets in a workbook when it loads"

Being a new person to this I might have not explained my self to well.
What I was trying to explain to you was that on you S/S when I put my mouse in any cell/input box I could not get a right mouse click
But I could get aright  mouse click on the sheets at the bottom, which I do not want
What I ment and mean is the following
The names at the bottom of the workbook where it list like  "sheet1   sheet2   sheet3"
so rightly or wrong I presumed they was called sheets.
I hope that has cleared that up.
Your comment
"  1) your question said "I have seached the internet and found a guy who said use this bit of code with Excel 2007"- why don't you ask him "
Dont you think I would have done that as first point of call?
it was a very old post that I found on google and he is no longer about.
Sorry run out of your restrictions on text will finish off on another one
kimzac (rep: 2) Dec 20, '23 at 8:06 pm
from above
Your Comment
2) specifically what are you trying to prevent the user from doing? (There may be other ways)..
stopping them from right mouse clicking on a sheet at bottom of workbook and selecting "view code"  then messing all the vba code up. Do you get it now?
Your Comment.
I won't be doing anything else on this until you clarify your question. I'll suggest Willie does likewise.
Ok that is upto you, but do not forget not every one is not has good as you and can not explain there self and do not know all the names and terms of things
Regards

Kim
kimzac (rep: 2) Dec 20, '23 at 8:08 pm
Kim, 
The code I suggested disable right mous click only for the workbook with the code, not all open file. To prevent users from messing with your vba code password protect the VBA project in the VB Editor window: "Tools" ==>> "VBAProject Properties" ==>> select the "Protection" tab ==>> check the box "Lock project for viewing" ==>> enter a password and confirm password. By doing this, to view the project and have access to the worksheets and modules the password will need to be entered.

If this solves your dilemma please mark my original answer as Selected.
If my answer is not satisfactory, I'm sorry but I don't know how else I can be of help and will be "tapping out"

Cheers   :-)
WillieD24 (rep: 557) Dec 20, '23 at 11:38 pm
@Kim (aka zacrock aka John S, I believe)- Willie has provided you with the way to realise your goals for the user of "stopping them from right mouse clicking on a sheet at bottom of workbook and selecting "view code"  then messing all the vba code up".

I'm pretty sure I suggested you do that in our direct correspondence but you thought the user wasn't Excel-savvy.

Anyway, Willie has advised you how to protect the code so please mark his Answer as Selected.

@Willie - thanks for telling the user how to protect the VBA code. Like you, I assumed it was a project where right-click in the sheet (or on cells) was to be stopped, given Excel is mostly about working inside sheets.

@Both - Have a good Christmas!
John_Ru (rep: 6142) Dec 21, '23 at 3:33 am
Add to Discussion
0

Hi Kim,

John has described where to put the code but I have different (simpler) code.

The code you found affects right click at the application level - all of Excel, every workbokk open while the workbook with the code is open. The code below only affects the workbook it is in. There is nothing that is turned off and then needs to be turned back on.

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

 Cancel = True

End Sub

To understand what this code means the line "Cancel = True" can be read as "cancel the right click for this workbook - Yes"

Hope this helps.

Cheers   :-)

Discuss

Discussion

Hi WillieD24
Sorry must have got it wrong somwhere
I did as John Ru Said and put it at the end of the end statment like this
'
  
  Next ws

    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub
THE ABOVE IS THE LAST BIT OF CODE

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

 Cancel = True

End Sub


I close workbook down and restart BUT right mouse still works

Kind Regards

Kim
kimzac (rep: 2) Dec 15, '23 at 4:18 pm
Hi Kim, 
I’m not clear as to where you put it but obviously it was the wrong place. I would suggest you remove it from wherever it is and do the following.
1)  Open the VB editor window (Alt + F11)
2)  In the “Project” window (top left) double click on “ThisWorkbook; this will open the code window where the “Private Sub Workbook_BeforeClose(Cancel As Boolean)” event resides.
3)  After “End Sub” of this code press “enter” a couple of times to create a space
4)  Paste the code here
Now when you close and re-open the workbook right click is disabled for the workbook.
If you still encounter problems let me know.

If this solves your issue please mark my answer above as selected.

Cheers   :-)
WillieD24 (rep: 557) Dec 15, '23 at 7:56 pm
Hi WillieD24

Yes that is what I did as you say above but did not work
When I doubled clicked on the workbook on the left then on the right side there is a load of vba code
Starts with
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Call ClsApp
End Sub
Then a load of vba code and ends with the below

'
            ws.Activate
'            Exit For
'        End If
'    Next ws
     Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub
I then after the end statement I put your vba code like below

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

 Cancel = True

End Sub

I hope I have explained my self to what I did

Kind Regards
Kim

kimzac (rep: 2) Dec 16, '23 at 4:22 am
Hi Kim,

The code which you show above is not part of the original file. This code could be what is causing you issues. I have attached a new version of my file (Ver 1B2) to my answer above. This version has the "RightClick" code added in the correct place and works perfectly for me. (Excel 2007, 2016)
If, when you open the file, you get a security warning (at the top, below the ribbon) stating "Macros have been disabled" there will be a button beside it, either "Enable" or "Options". If it is "Enable" - click it; if it is "Options" click it and select "Enable this content" and then "Ok". If macros are not enabled the file will not behave as expected.

If you still have difficulties, would it be possible for you to upload your file (or copy) without any personal or identifying info so we can inspect it and hopefully detemine where the problem lies?
Cheers   :-)
WillieD24 (rep: 557) Dec 16, '23 at 9:53 am
WillieD24
I might have posted this twice sorry if have.
Sorry to say that file (Protect UnProtect - Ver 1B2.xlsm)
did not work.
I have made a new workbook and pasted that code in the vba workbook.
I have sent it to you or to this site I hope for you to test on your version 2007.
Because it just does not work on mine for some reason
Kind Regards

KIM
kimzac (rep: 2) Dec 16, '23 at 3:58 pm
Hi Kim,

Sorry to hear that you keep running into snags. Thanks for adding a file to your original post. I downloaded it and tested it with 2007 and 2016 - no issues - right click was disabled. Seeing this, I suspect your troubles are related to your macro security settings. Your macro security may be set to disable all macros. 
To check your settings:
1) open any file or a blank workbook
2) click the round logo at top left
3) now at the bottom right click "Excel Options"
4) in the list at the left click "Trust Center"
5) in the list at the left click "Trust Center Settings"
6) click "Macro Settings"
I suspect the first option - "Disable all macros without notification" is selected. Change this to the second option - "Disable all macros with notification"
Close Excel and open your "r mouse.xlsm" file and it should have the right click disabled.

Cheers   -:)
WillieD24 (rep: 557) Dec 16, '23 at 6:39 pm
WillieD24

Sorry to tell you this BUT i have done that already
I have doubled check it and yes they are enabled and I have run macros all okay

I'm just lost to why it will not work whenit works on your computer

KIM
kimzac (rep: 2) Dec 16, '23 at 7:22 pm
Kim,

Sorry, but I don't have any other suggestions at this point. If I discover something I will get back to you.
If I understand what you have said, you have other files with macros and they work as they should?
WillieD24 (rep: 557) Dec 16, '23 at 8:20 pm
Kim,

Just remembered this. I had a similar issue with a particular file a while ago. All other files with macros were fine except one.
To resolve the problem I had to do the following:
- go into Trust Center ==>> Trusted Locations and add the file path for the problem file as a trusted location. After that everything was fine.
I have no explanation why this one file needed this treatment. Give it a try.

Cheers   :-)
WillieD24 (rep: 557) Dec 16, '23 at 8:50 pm
WillieD24
Sorry but it cannot be that as like i said I made a NEW file, and just to be shure I have made 3 new one just the same

Kim
kimzac (rep: 2) Dec 17, '23 at 2:59 am
Kim,

I'm out of possible reasons.
Hopefully someone else will see this post without a selected answer and chime in with a possible solution.
Good Luck.
WillieD24 (rep: 557) Dec 17, '23 at 9:28 am
WilllieD24
Many Thanks for trying any way
It is most strange what I'm going to do is try it on my other computer and see what I get on that.

will let you know how I get on.

That other chap who post on here might see it John xxx somebody

Kim
kimzac (rep: 2) Dec 17, '23 at 10:37 am
Hi WillieD24

I have put it on my other three computers and still does not work.
Even took it my son's computer and does not worlk  on his
You must have some thing different on your computer to what we have for yours to  work.
Be interesting if John Ru looks at it and try it on hiis computer

Kim
kimzac (rep: 2) Dec 17, '23 at 7:42 pm
Kim,
Is this the only file with macros that doesn't behave as expected?
WillieD24 (rep: 557) Dec 17, '23 at 8:08 pm
WillieD24
It is just not the one file .
As I say I make a NEW file from when you say new file in excell.
so there is no vba in the file untill I paste your code in it.
I even made new files on my other computers and on my sons computer.
What I have done now is just to prove that macros are working on the file I have downloaded som vba code that uses on first start up and that works so nothing to do with macros etc.
I realy want this mouse thing to work

KIM
kimzac (rep: 2) Dec 18, '23 at 5:28 am
Kim

I just tried Willie's file on my PC and the right mouse click is disabled.over cells (as expected). I also created a blank file, pasted Willie's code:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
 
 Cancel = True
 
End Sub
into the ThisWorkbook section of VBA (it MUST be there in a single file) and that disables the right mouse click too (apart from.over the Ribbon). Hope you can replicate this.

Regarding copying code in new files, you shouldn't need to on your PC- see Don's tutorial Make Your Macros Available in All Workbooks in Excel
John_Ru (rep: 6142) Dec 18, '23 at 2:14 pm
John_Ru
Thank you for trying to help with this problem
I have a load of code in the "thisworkbook"  so do not get your statement "single file"
So what I have done is made a new workbook and the put willieD24 code in that thisworkbook.  BUT still does not work.  Please the sample workbook file that I uploaded.

KIM
kimzac (rep: 2) Dec 20, '23 at 5:14 am
Kim

Please see my Revised answer 20 December 2023 and associated file. (My comment about "single file" should make sense).

If that works for you, please mark my Answer as selected. If not, please edit your question to add your file with other macros - if they work, so should my solution (and Wille's, come to that).
John_Ru (rep: 6142) Dec 20, '23 at 9:04 am
@Willie - Kim had issues with my revised Answer but the comments prompted other questions from me. I suggest you look at the Discussion today (20 December) under my Answer and decide whether or not to pursue this for now.
John_Ru (rep: 6142) Dec 20, '23 at 3:31 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login