Excel 2010中的自定义其他单元格操作

时间:2011-09-27 13:44:01

标签: excel-2010

我想通过添加更多“附加单元格操作”来扩展MS Excel 2010。 (可通过单元格右键单击>附加单元格操作访问)。具体来说,我想要Excel:

  1. 将五到八位数字识别为具有操作的部件号:“打开技术文档的URL”
  2. 将字符串“OR ## #####”(#for digit)识别为带有操作的订单参考:“打开规范文件”和“打开材料文件”(位于Intranet中指定路径的两个Excel文件)< / LI>

    现在,我不知道如何编程。我怀疑需要一些XML代码段,也可能是一些VB代码。 VB代码不会是一个问题 - 我有宏为Excel 2003完成这些功能 - 但我不知道在哪里放置它。

    请给我一些指示,我已经问谷歌但是无法得到答案,似乎“附加行动”是很常见的短语:)

1 个答案:

答案 0 :(得分:1)

这可以通过向工作簿添加右键单击事件处理程序来实现

在工作簿模块中添加此代码

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Dim cBut As CommandBarButton
    Dim v As Variant

    On Error Resume Next

    v = Target

    ' Remove any previously added menu items
    Application.CommandBars("Cell").Controls("Open URL to technical docs").Delete
    Application.CommandBars("Cell").Controls("Open material file").Delete

    ' save cell value for use by called macro
    CellValue = v

    ' If cell matches criteria add menu item and set macro to call on click
    If IsNumeric(v) Then
        If v >= 10000 And v <= 99999999 Then
            Set cBut = Application.CommandBars("Cell").Controls.Add(Temporary:=True)

            With cBut
                .Caption = "Open URL to technical docs"
                .Style = msoButtonCaption
                .OnAction = "OpenRef"
            End With
        End If
    ElseIf v Like "OR ## #####" Then
        Set cBut = Application.CommandBars("Cell").Controls.Add(Temporary:=True)

        With cBut
            .Caption = "Open material file"
            .Style = msoButtonCaption
            .OnAction = "OpenMat"
        End With
    End If
End Sub

在标准模块中添加此代码

Public CellValue As Variant

' replace MsgBox code with your logic to open files
Sub OpenRef()
    MsgBox "Open Reference Doc code here for " & CellValue
End Sub

Sub OpenMat()
    MsgBox "Open Material File code here for " & CellValue
End Sub