vba活用excel右键菜单

116 篇文章 0 订阅
24 篇文章 0 订阅

仅在a列出现数据菜单:

thisworkbook代码:

Option Explicit
Private Sub Workbook_Deactivate()
    Call DeleteMycell
End Sub

sheet1 代码:

Option Explicit
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 1 Then
        Call Mycell
        Application.CommandBars("Mycell").ShowPopup
        Cancel = True
    End If
End Sub

 

新建立模块代码:

Option Explicit
Sub Mycell()
    Dim arr As Variant
    Dim i As Integer
    Dim Mycell As CommandBar
    On Error Resume Next
    Application.CommandBars("Mycell").Delete
    arr = Array("经理室", "办公室", "生技科", "财务科", "营业部")
    Set Mycell = Application.CommandBars.Add("Mycell", 5)
    For i = 0 To 4
        With Mycell.Controls.Add(1)
            .Caption = arr(i)
            .OnAction = "MyOnAction"
        End With
    Next
End Sub
Sub MyOnAction()
    ActiveCell = Application.CommandBars.ActionControl.Caption
End Sub
Sub DeleteMycell()
    On Error Resume Next
    Application.CommandBars("Mycell").Delete
End Sub

 


---------------------

改变整个右键菜单代码:

thisbook里:

Option Explicit
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Application.CommandBars("Mycell").ShowPopup
    Cancel = True
End Sub

 

sheet1里:


Option Explicit
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Application.CommandBars("Mycell").ShowPopup
    Cancel = True
End Sub

新建模块里:

Option Explicit
Sub Mycell()
    With Application.CommandBars.Add("Mycell", msoBarPopup)
        With .Controls.Add(Type:=msoControlButton)
            .Caption = "会计凭证"
            .FaceId = 9893
        End With
        With .Controls.Add(Type:=msoControlButton)
            .Caption = "会计账簿"
            .FaceId = 284
        End With
        With .Controls.Add(Type:=msoControlPopup)
            .Caption = "会计报表"
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "月报"
                .FaceId = 9590
            End With
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "季报"
                .FaceId = 9591
            End With
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "年报"
                .FaceId = 9592
            End With
        End With
        With .Controls.Add(Type:=msoControlButton)
            .Caption = "凭证打印"
            .FaceId = 9614
            .BeginGroup = True
        End With
        With .Controls.Add(Type:=msoControlButton)
            .Caption = "账簿打印"
            .FaceId = 707
        End With
        With .Controls.Add(Type:=msoControlButton)
            .Caption = "报表打印"
            .FaceId = 986
        End With
    End With
End Sub
Sub DeleteMycell()
    On Error Resume Next
    Application.CommandBars("Mycell").Delete
End Sub

 

其他的sheet里:

Option Explicit

-------------------

 

禁用鼠标右键:


thisworkbook里:

Option Explicit
Private Sub Workbook_Deactivate()
    Call EnaBar
End Sub

新建模块:

Option Explicit
Sub DisBar()
    Dim myBar As CommandBar
    For Each myBar In CommandBars
        If myBar.Type = msoBarTypePopup Then
            myBar.Enabled = False
        End If
    Next
End Sub
Sub EnaBar()
    Dim myBar As CommandBar
    For Each myBar In CommandBars
        If myBar.Type = msoBarTypePopup Then
            myBar.Enabled = True
        End If
    Next
End Sub
在sheet中定义2个command分别指定宏,可实现禁用与启用。

 

-------------------

高级自定义右键菜单项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值