Wednesday, March 14, 2012

Sample code for BeforeRightClick Event

Private Sub Worksheet_BeforeRightClick(ByVal Target As Excel.Range, Cancel As Boolean)

Dim cBar As CommandBar
Dim cmdCon1 As CommandBarControl
Dim cmdCon2 As CommandBarControl

'Prevent the standard popup showing
 Cancel = True

'We must delete the old one first
On Error Resume Next
Application.CommandBars("Ozgrid").Delete

    'Add a CommandBar and set the CommandBar _
     variable to a new popup
     Set cBar = Application.CommandBars.Add _
     (Name:="Ozgrid", Position:=msoBarPopup)

    'Add a control and set our 1st CommandBarControl _
     variable to a new control
     Set cmdCon1 = cBar.Controls.Add
    'Add some text and assign the Control
    With cmdCon1
        .Caption = "I'm a custom control"
        .OnAction = "MyMacro" 'calling macro from Module
    End With

    'Add a control and set our 2nd CommandBarControl _
     variable to a new control
     Set cmdCon2 = cBar.Controls.Add
    'Add some text and assign the Control
    With cmdCon2
        .Caption = "Somu's Macro"
        .OnAction = "AnotherMacro"
    End With

cBar.ShowPopup
End Sub


No comments:

Post a Comment