Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to add custom button to the right click/context menu in Excel?

As below screenshots shown, many people tend to add their frequently-used functions to Excel’s right click menu in order to quickly use these functions with just one click in the future. This article is talking about adding custom code button to the right click menu in Excel.

Add custom button to the context menu in Excel with VBA code

Excel Productivity Tools

Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial

Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately. 60-day Unlimited Free Trial


arrow blue right bubbleAdd custom button to the context menu in Excel with VBA code


Supposing you have created a VBA script named MyMacro in your Excel, and now you need to add this function to the context menu for running the code just by clicking it. Please do as follows.

1. Press the Alt + F11 keys simultaneously to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, double click ThisWorkbook in the left Project pane. And then copy and paste the below VBA code into the ThisWorkbook (Code) window.

VBA code: Add custom button to the context menu in Excel

Private Sub Workbook_Deactivate()
    On Error Resume Next
        With Application
            .CommandBars("Cell").Controls("MyMacro").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 cmdBtn As CommandBarButton
    On Error Resume Next
        With Application
            .CommandBars("Cell").Controls("MyMacro").Delete
            Set cmdBtn = .CommandBars("Cell").Controls.Add(Temporary:=True)
        End With

        With cmdBtn
           .Caption = "MyMacro"
           .Style = msoButtonCaption
           .OnAction = "MyMacro"
        End With
    On Error GoTo 0
End Sub

Note: In the code, please replace all “MyMacro” with the Macro name you have created in your Excel.

3. Press the Alt + Q keys to exit the Microsoft Visual Basic for Applications window.

Now you return to the Excel interface, after right-clicking on a cell, you will see the new button with the specified name is added into the right-clicking menu as below screenshot shown. Click the button will trigger the corresponding Macro immediately.


arrow blue right bubbleRelated articles:


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  • Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  • Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  • Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  • Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  • No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  • Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  • 60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  • Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    MFouad · 6 months ago
    Thanks
    Does that working for all workbooks even after restarting Excel, or only for the selected workbook which saved the macro in?
    • To post as a guest, your comment is unpublished.
      Виктор · 3 days ago
      Размести код в отдельном модуле, для того чтобы это работало для всех документов
    • To post as a guest, your comment is unpublished.
      crystal · 6 months ago
      Hi,
      It only works for the workbook which save the macro in. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Vic · 1 years ago
    Add VBA button to context menu works well. Thanks. How do I add more buttons ? (not good at VBA, hence the question).
    • To post as a guest, your comment is unpublished.
      crystal · 6 months ago
      Hi Vic,
      The following VBA code can help you solve the problem, please have a try.

      Private Sub Workbook_Deactivate()
      Dim xArrB As Variant
      Dim xFNum As Integer
      Dim xStr As String
      On Error Resume Next
      With Application
      xArrB = Array("MyMacro01", "MyMacro02", "MyMacro03")
      For xFNum = 0 To UBound(xArrB)
      xStr = xArrB(xFNum)
      .CommandBars("Cell").Controls(xStr).Delete
      Next xFNum
      End With
      On Error GoTo 0
      End Sub

      Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
      Dim cmdBtn As CommandBarButton
      Dim xArrB As Variant
      Dim xFNum As Integer
      Dim xStr As String
      On Error Resume Next
      xArrB = Array("MyMacro01", "MyMacro02", "MyMacro03")
      For xFNum = 0 To UBound(xArrB)
      xStr = xArrB(xFNum)
      With Application
      .CommandBars("Cell").Controls(xStr).Delete
      Set cmdBtn = .CommandBars("Cell").Controls.Add(Temporary:=True)
      End With
      With cmdBtn
      .Caption = xStr
      .Style = msoButtonCaption
      .OnAction = xStr
      End With
      Next xFNum
      On Error GoTo 0
      End Sub
      • To post as a guest, your comment is unpublished.
        Mike · 2 months ago
        This doesn't work for me, I have done exactly the same as the above macro with only one menu, Does it work for anyone else or am I doing something wrong?

        Thanks