Add A Right-click Menu To A User Form


June 21, 2021 - by

Add A Right-click Menu To A User Form

Challenge: You’ve designed a custom user form. You want to add a right-click menu (also called a context menu) to it.

Solution: Many people are used to right-clicking in Excel. If you’ve designed a custom user form, people might want to right-click the form to get additional options. A solution Haluk posted at the MrExcel message board takes advantage of the user form’s MouseDown event. This event is run when someone clicks on the user form. A variable called Button indicates whether the left or right mouse button is clicked. Another variable, Shift, indicates which combinations of the Shift, Ctrl, and Alt keys are held down.

For the Button variable, 1 indicates a left-click, and 2 indicates a right-click.


For the Shift variable, 0 indicates that no keys are used in combination with the mouse click, 1 indicates the Shift key, 2 indicates the Ctrl key, and 4 indicates the Alt key. If someone holds down multiple keys, Excel adds the values (for example, 5 indicates Alt+Shift). Here is the complete table of possible values for the Shift variable:

Shift Value Meaning
0 No keys
1 Shift
2 Ctrl
3 Shift+Ctrl
4 Alt
5 Shift+Alt
6 Ctrl+Alt
7 Shift+Ctrl+Alt


The event handler macro first makes sure that Button is 2 and Shift is 0.

The macro uses API calls to build a menu. You can customize the menu by adding additional options to the menu, using:

AppendMenu hMenu, MF STRING, 1, “Menu Text Here”

In this case, the 1 indicates the value returned to the macro if that menu item is selected.

The TrackPopupMenu function displays the menu and determines which item is selected by the person using the form. The selection is returned to the variable ret. After this line of code, the ret variable indicates the numeric menu value selected.

Later in the macro, a Case Select handles the possible menu choices and calls an appropriate procedure for each.

Place all this code in the code pane for the user form:

e9781615474011_i0293.jpg
e9781615474011_i0294.jpg
e9781615474011_i0295.jpg

When someone right-clicks the form, the menu is displayed (Figure 146).

Figure 146. The custom right-click menu is displayed.
Figure 146. The custom right-click menu is displayed.

Summary: You can add right-click menu functionality to a user form.

Source: need a right click menu on the userform---> VBA on the MrExcel Message Board.

This topic was nominated by Microsoft MVP Greg Truby.

Title Photo: Guillaume Issaly on Unsplash


This article is an excerpt from Excel Gurus Gone Wild.