vba活用excel右键菜单

本文详细介绍了如何在Excel中自定义右键菜单,包括在特定列出现数据时显示特定菜单项,并提供了禁用和启用鼠标右键菜单的功能。通过创建命令按钮和弹出式菜单,用户可以实现更加个性化的操作体验。

仅在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分别指定宏,可实现禁用与启用。

 

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

高级自定义右键菜单项

虽然给定引用中未直接提及通过Excel VBA右键菜单打开自定义窗口的具体方法,但可以结合相关知识给出通用步骤。 ### 实现思路 1. **创建自定义窗口**:在VBA中,可通过创建用户窗体来作为自定义窗口。在VBA编辑器里,选择 “插入” -> “用户窗体”,然后可以在窗体上添加各种控件,如文本框、按钮等。 2. **自定义右键菜单**:可以使用VBA代码来创建一个自定义的右键菜单。 3. **关联右键菜单与自定义窗口**:在右键菜单的点击事件里,添加打开自定义窗口的代码。 ### 示例代码 ```vba Sub CreateCustomRightClickMenu() Dim cbar As CommandBar Dim cbarCtrl As CommandBarControl ' 删除已有的自定义右键菜单(若存在) On Error Resume Next Application.CommandBars("Cell").Controls("Open Custom Window").Delete On Error GoTo 0 ' 创建自定义右键菜单项 Set cbar = Application.CommandBars("Cell") Set cbarCtrl = cbar.Controls.Add(Type:=msoControlButton, Temporary:=True) cbarCtrl.Caption = "Open Custom Window" cbarCtrl.OnAction = "OpenCustomWindow" End Sub Sub OpenCustomWindow() ' 显示用户窗体(自定义窗口) UserForm1.Show End Sub ``` ### 代码解释 1. **CreateCustomRightClickMenu 过程**:此过程用于创建自定义的右键菜单项。它先删除已有的同名菜单项(若存在),接着在单元格的右键菜单里添加一个名为 “Open Custom Window” 的新菜单项,并且将其点击事件关联到 `OpenCustomWindow` 过程。 2. **OpenCustomWindow 过程**:该过程用于显示用户窗体(自定义窗口)。这里假设用户窗体的名称是 `UserForm1`。 ### 使用方法 1. 打开ExcelVBA编辑器(可通过 “开发工具” 菜单 -> “Visual Basic” 打开)。 2. 插入一个新的模块(右键项目 -> 新建模块)。 3. 将上述代码复制到新模块中。 4. 插入一个用户窗体(插入 -> 用户窗体),可根据需求在窗体上添加控件。 5. 运行 `CreateCustomRightClickMenu` 过程,这样在单元格上右键点击时,就会出现 “Open Custom Window” 菜单项,点击该菜单项即可打开自定义窗口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值