Ozgrid, Experts in Microsoft Excel Spreadsheets
Javascript DHTML Drop Down Menu Powered by dhtml-menu-builder.com
Learn how to create Excel dashboards.

Add Excel Right Click Menu

Information Helpful? Why Not Donate.

TRY OUT: Smart-VBA | Code-VBA | Analyzer-XL | Downloader-XL | Trader-XL | More Free Downloads.. Best Value: Finance Templates Bundle

Excel VBA: Adding a Command Button to the Excel Right Click Pop-up Toolbar/Command Bar

Current Special! Complete Excel Excel Training Course for Excel 97 - Excel 2003, only $145.00. $59.95 Instant Buy/Download, 30 Day Money Back Guarantee & Free Excel Help for LIFE!

Adding a Command Button to the Excel Right Click Pop-up Toolbar/Command Bar

See Also: Create Custom Excel Menu Items || Hide/Restore Excel Toolbars || Quick Access to Excel Custom Lists
Excel allows us to modify nearly every part of Excel. One of these parts is the standard Excel CommandBars, or Toolbars. Sometimes you may have a macro that you wish to enable users to run by right clicking in any cell and choosing your macro from the Cell shortcut pop-up menu. The code do do this is relatively simply and is shown below.

Note how the code first deletes any existing menu option called "My Macro". This stops the code adding an additional custom menu options and ensures only one custom menu option is available at any one time. The use of Temporary:=True ensures this menu option is deleted whenever Excel closes. Without it, it's possible to have you custom menu option showing in another Workbook. This is also why we make use of the Workbook_Deactivate Event. That is, if we activate another Workbook while this one is open, the custom menu option is deleted.

The macro that is run whenever this custom menu option is clicked is called "My_Macro". You should change this, and the caption, to suit your needs.

The code below should be placed in Private Module of the Workbook Object (ThisWorkbook). To get there quickly, right click on the Excel icon, top left next to File and choose View Code.

Private Sub Workbook_Deactivate()

    On Error Resume Next

            With Application

                .CommandBars("Cell").Controls("My Macro").Delete

            End With

    On Error GoTo 0

End Sub





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

Dim cBut As CommandBarButton



    On Error Resume Next

        With Application

            .CommandBars("Cell").Controls("My Macro").Delete

            Set cBut = .CommandBars("Cell").Controls.Add(Temporary:=True)

        End With

        

        With cBut

           .Caption = "My Macro"

           .Style = msoButtonCaption

           .OnAction = "My_Macro"

        End With

    On Error GoTo 0

End Sub

New & Less Than You Think: List Managers | Working With Excel Sheets In VBA | Excel Charting Lessons | Delete rows by condition | TRY OUT: Smart-VBA | Code-VBA | Analyzer-XL | Downloader-XL | Trader-XL | More Free Downloads.. Best Value: Finance Templates Bundle

Excel Dashboard Reports & Excel Dashboard Charts 50% Off Become an ExcelUser Affiliate & Earn Money

Special! Free Choice of Complete Excel Training Course OR Excel Add-ins Collection on all purchases totaling over $64.00. ALL purchases totaling over $150.00 gets you BOTH! Purchases MUST be made via this site. Send payment proof to special@ozgrid.com 31 days after purchase date.


Instant Download and Money Back Guarantee on Most Software

Try out: Analyzer XL | Downloader XL | Smart VBA | Trader XL Pro (best value) | ConsoXL | MergeXL | O2OLAP for Excel | MORE>>

Excel Trader Package Technical Analysis in Excel With $139.00 of FREE software!

Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft

GIVE YOURSELF OR YOUR COMPANY 24/7 MICROSOFT EXCEL SUPPORT & QUESTIONS