I'm making a workbook to be used by many other people, and so the workbook is protected and most of the sheets are protected. Because of this, I want to eliminate several items from the cell right-click menu, since they will always be greyed out. I was able to get most of the items removed, with two exceptions. See below.
Here is the code I used to remove the items.
You will notice I have code to remove "Insert Cells". But perhaps just "Insert" is a different ID?
I also have one in that should hide "Link to this Range", but perhaps "Link" is also a different ID?
I used some other code to generate a list of all of the right-click options, along with their ID's, on both Windows and Mac. (My workbook needs to work on both.) I created this just the other day, so I believe all of the ID's are current.
(I don't know why one of the columns above is so much wider than the others. I don't know how to change it.)
There is no listing for "Insert" or "Link", so I am not sure how I am supposed to know what the ID number is for those. So, I'm at a loss for how I would hide those two. Any ideas?
Here is the code I used to remove the items.
VBA Code:
Sub RCM_HideItems()
Dim RCM As CommandBar
' Access the Cell context menu (right-click menu)
Set RCM = Application.CommandBars("Cell")
On Error Resume Next
' Hide the "Cut" option
RCM.FindControl(ID:=21).Visible = False
' Hide the "Insert Cells" (called "Cells" on Mac) option
RCM.FindControl(ID:=295).Visible = False
' Hide the "Delete" option
RCM.FindControl(ID:=292).Visible = False
' Hide the "Clear Contents" option
RCM.FindControl(ID:=3125).Visible = False
' Hide the "Filter" option
RCM.FindControl(ID:=31402).Visible = False
' Hide the "Sort" option
RCM.FindControl(ID:=31435).Visible = False
' Hide the "Pick From Drop-down List" option
RCM.FindControl(ID:=1966).Visible = False
' Hide the "Define Name" option
RCM.FindControl(ID:=13380).Visible = False
' Hide the "Link to this Range" option
RCM.FindControl(ID:=34646).Visible = False
' Hide the "Open Hyperlink" option
RCM.FindControl(ID:=1015).Visible = False
'These items are only for Windows.
If Not IsMac Then
' Hide the "Quick Analysis" option
RCM.FindControl(ID:=24508).Visible = False
' Hide the "Get Data from Table/Range" option
RCM.FindControl(ID:=34003).Visible = False
End If
On Error GoTo 0
Set RCM = Nothing
End Sub
You will notice I have code to remove "Insert Cells". But perhaps just "Insert" is a different ID?
I also have one in that should hide "Link to this Range", but perhaps "Link" is also a different ID?
I used some other code to generate a list of all of the right-click options, along with their ID's, on both Windows and Mac. (My workbook needs to work on both.) I created this just the other day, so I believe all of the ID's are current.
Control ID | ||
Control Name | Windows | Mac |
Cu&t | 21 | 21 |
&Copy | 19 | 19 |
&Paste | 22 | 22 |
Paste &Special... | 21437 | 755 |
&Paste Table | 3624 | |
Smart &Lookup | 25536 | 25536 |
&Show Data Type Card | 32714 | 32714 |
Data T&ype | 32713 | 32713 |
&Picture in Cell | 34738 | 34738 |
View &Alt Text... | 34737 | 34737 |
&Copilot | 34820 | 34820 |
Insert C&ells... | 295 | 295 |
&Delete... | 292 | 292 |
Clear Co&ntents | 3125 | 3125 |
Translate | 33409 | 33409 |
P&ython Output | 34750 | 34750 |
Display &Plot over Cells | 45242 | 35077 |
Show &Diagnostics | 34713 | 34713 |
&Quick Analysis | 24508 | |
Sp&arklines | 31623 | 31623 |
Filt&er | 31402 | 31402 |
S&ort | 31435 | 31435 |
&Get Data from Table/Range... | 34003 | |
Insert Co&mment | 2031 | 2031 |
Delete Co&mment | 1592 | 1592 |
Sh&ow/Hide Comments | 1593 | 1593 |
&Format Cells... | 855 | 855 |
Pic&k From Drop-down List... | 1966 | 1966 |
&Show Phonetic Field | 1614 | 1614 |
Define N&ame... | 13380 | 13380 |
&Open with | 34600 | 34600 |
Set o&pen default | 34680 | 34680 |
Set O&pen Default | 49649 | 35174 |
&Hyperlink... | 1576 | 1576 |
Edit &Hyperlink... | 1577 | 1577 |
&Open Hyperlink | 1015 | 1015 |
&Remove Hyperlink | 3626 | 3626 |
&Link to this Range | 34646 | 34646 |
Show Chan&ges | 34405 | 34405 |
E&xpand to detail | 11299 | 11299 |
Additional Act&ions | 31595 | 31595 |
F&ull Screen | 178 | 178 |
Hide Ot&hers | 34125 | 34125 |
&Additional Actions | 22577 | |
People &Near Me | 34042 | 34042 |
&Thesaurus... | 9056 | |
New Co&mment | 33164 | |
&New Note | 33158 |
(I don't know why one of the columns above is so much wider than the others. I don't know how to change it.)
There is no listing for "Insert" or "Link", so I am not sure how I am supposed to know what the ID number is for those. So, I'm at a loss for how I would hide those two. Any ideas?