Excel加载项无法正常运行

时间:2016-08-01 23:01:25

标签: vba excel-vba macros add-in excel-addins

我正在尝试创建一个excel加载项,允许我为列中的所有条目添加前缀。

我想我想出了一些代码来执行此操作,但不幸的是我一直收到一条错误消息“此宏可能在此工作簿中不可用,或者所有宏都可能被禁用。我已经尝试了所有建议的禁用安全性设置。

我希望能够简单地选择文本,使用加载项,并为每一列添加前缀。干杯并非常感谢您对此进行调查!

以下是代码:

Private Sub Workbook_AddinInstall()



On Error Resume Next 'Just in case

'Delete any existing menu item that may have been left.

Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete

'Add the new menu item and Set a CommandBarButton Variable to it

Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add

'Work with the Variable

    With cControl

        .Caption = "Super Code"

        .Style = msoButtonCaption

        .OnAction = "MyGreatMacro"

        'Macro stored in a Standard Module

    End With



On Error GoTo 0


End Sub
Private Sub Workbook_AddinUninstall()



On Error Resume Next 'In case it has already gone.

Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete

On Error GoTo 0

End Sub

Private Sub AddTextOnLeft()
'Updateby20131128
Dim Rng As Range
Dim WorkRng As Range
Dim addStr As String
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
addStr = Application.InputBox("Add text", xTitleId, "", Type:=2)
For Each Rng In WorkRng
Rng.Value = addStr & Rng.Value
Next
End Sub

1 个答案:

答案 0 :(得分:0)

对于你已经分配了'MyGreatMacro'而不是'AddTextOnLeft'的OnAction

应该是

.OnAction = "AddTextOnLeft"

而不是

.OnAction = "MyGreatMacro"