2

is there an easy way to enable the right click pop up menu in a text box in a user form in vba, excel? Im just trying to paste into a textbox but cant open the menu on right click in a user form

my code is giving a "Compile error: Argument not optional" and highlighting the .Add in the With Controls.Add ...

 Private myMenu

Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If (Button = 2) Then myMenu.ShowPopup
End Sub

Private Sub UserForm_Initialize()
    With UserForm1
        .StartUpPosition = 2
    End With

    Set myMenu = Application.CommandBars.Add(Position:=msoBarPopup, Temporary:=True)

    With myMenu
        With Controls.Add
            .Caption = "Hello"
            .OnAction = "HelloWorld"
        End With
    End With
End Sub
  • 1
    have a look here: https://social.msdn.microsoft.com/Forums/en-US/663c4553-03e8-4a41-8fbc-2ff0e4a41922/right-click-menu-in-userform-textbox-for-excel-to-copy-past-or-cut?forum=exceldev – sous2817 Aug 16 '21 at 17:12
  • yeah i found that before but keep running into "Compile error: Argument not optional" and it highlights .Add in this part of the code With myMenu With Controls.Add .Caption = "Hello" .OnAction = "HelloWorld" End With End With.... PS not sure how to edit this as code sorry – Jacob Mazanowski Aug 17 '21 at 13:10
  • 1
    if you edit your post and include what you tried and which line you're getting the error, more people will be able to help. – sous2817 Aug 17 '21 at 13:22
  • oh yeah good call – Jacob Mazanowski Aug 17 '21 at 13:24

1 Answers1

2

Change this line:

With Controls.Add

To:

With .Controls.Add
RBarryYoung
  • 55,398
  • 14
  • 96
  • 137