gpt4 book ai didi

vba - 无法在 Excel 中运行使用加载项创建的宏

转载 作者:行者123 更新时间:2023-12-02 22:16:55 24 4
gpt4 key购买 nike

我创建了一个xla(excel插件),它具有保护文档的功能(以便用户可以在不知道密码的情况下保护文档)。此 xla 添加到需要此功能的每个 Excel 文件中。

当xla安装或添加到excel时,保护按钮将添加到最后一个菜单中。但是当我单击按钮时,出现错误表明

“无法运行宏 Pivot Add-In 0.2'!protectSheet'”。该宏可能在此工作簿中不可用,或者所有宏都可能被禁用。”

事件处理程序onclicked的代码是protectSheet,请参阅下面的源代码:

有人能指出我为什么会出现这个问题吗?

在此工作簿上

'main function'
Public Sub protectSheet()
ActiveWorkbook.ActiveSheet.protect Password:="password", AllowUsingPivotTables:=True
End Sub

Public Sub ShowToolbar()
' Assumes toolbar not already loaded '
Application.CommandBars.Add Module1.TOOLBARNAME
AddButton "Proteksi Sheet", "Memproteksi Pivot", 3817, "protectSheet"

' call AddButton more times for more buttons '
With Application.CommandBars(Module1.TOOLBARNAME)
.Visible = True
.Position = msoBarTop
End With
End Sub

Public Sub AddButton(caption As String, tooltip As String, faceId As Long, methodName As String)
Dim Btn As CommandBarButton
Set Btn = Application.CommandBars(Module1.TOOLBARNAME).Controls.Add
With Btn
.Style = msoButtonIcon
.faceId = faceId ' choose from a world of possible images in Excel: see http://www.ozgrid.com/forum/showthread.php?t=39992 '
.OnAction = methodName
.TooltipText = tooltip
End With
End Sub

Public Sub DeleteCommandBar()
Application.CommandBars(TOOLBARNAME).Delete
End Sub



'called when add-in installed
Private Sub Workbook_AddinInstall()
Call ShowToolbar
End Sub

'called when add-in uninstalled
Private Sub Workbook_AddinUninstall()
Call DeleteCommandBar
End Sub

在模块1上

Public Const TOOLBARNAME = "PivotTools"

最佳答案

将所有函数移动到 module1 后,然后在 ThisWorkbook 上保留调用者函数,错误现在消失了。似乎我必须在同一个文件中定义调用/使用常量(Public Const TOOLBARNAME =“PivotTools”)的所有功能(在我的情况下是在 module1 中)

在 module1 文件上

Public Const TOOLBARNAME = "PivotTools"

'caller method'
Public Sub protectDoc()


On Error GoTo errorInfo
protectSheet

'if success, show msg box'
MsgBox ("Report berhasil diproteksi")

Exit Sub

errorInfo:
MsgBox Err.Description & vbCrLf & Err.Number

End Sub


Public Sub protectSheet()
ActiveWorkbook.ActiveSheet.protect Password:="password", AllowUsingPivotTables:=True
End Sub

Public Sub refreshDoc()

On Error GoTo errorInfo

refreshConnection
protectSheet
'if success, show msg box'
MsgBox ("Report berhasil diperbaharui")

Exit Sub

errorInfo:
MsgBox Err.Description & vbCrLf & Err.Number

End Sub


Private Sub refreshConnection()
ActiveWorkbook.ActiveSheet.Unprotect Password:="password"
ActiveWorkbook.RefreshAll
End Sub


Public Sub ShowToolbar()
' Assumes toolbar not already loaded '
Application.CommandBars.Add TOOLBARNAME
AddButton "Proteksi Sheet", "Memproteksi Pivot", 225, "protectDoc"
AddButton "Refresh Data", "Refresh Pivot", 459, "refreshDoc"

' call AddButton more times for more buttons '
With Application.CommandBars(TOOLBARNAME)
.Visible = True
.Position = msoBarTop
End With
End Sub

Public Sub AddButton(caption As String, tooltip As String, faceId As Long, methodName As String)
Dim Btn As CommandBarButton
Set Btn = Application.CommandBars(TOOLBARNAME).Controls.Add
With Btn
.Style = msoButtonIcon
.faceId = faceId
' choose from a world of possible images in Excel: see http://www.ozgrid.com/forum/showthread.php?t=39992 '
.OnAction = methodName
.TooltipText = tooltip
End With
End Sub

Public Sub DeleteCommandBar()
Application.CommandBars(TOOLBARNAME).Delete
End Sub

在此工作簿上

'called when add-in installed
Private Sub Workbook_AddinInstall()
Call Module1.ShowToolbar
End Sub

'called when add-in uninstalled
Private Sub Workbook_AddinUninstall()
Call Module1.DeleteCommandBar
End Sub

关于vba - 无法在 Excel 中运行使用加载项创建的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7396195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com