在我写这篇文章的时候,这个问题已经很老了(持续了 5 年),但是这个问题为我提供了完全解决这个问题所需的信息。我能够接受@jerryact 提供的答案并对其进行扩展以解决我的问题。
首先,与原始海报类似,我在 Workbook_Open 事件中注册了我的 UDF,当它作为加载项加载时,会导致在打开 Excel 时显示 Run-time error '1004': Cannot edit a macro on a hidden workbook. Unhide the workbook using the Unhide command. 错误消息。
使用@jerryact 上面答案中的信息,我将代码更改为如下所示:
Option Explicit
Private WithEvents App As Application
Private Sub Workbook_BeforeClose(Cancel As Boolean)
modUDFs.UnregisterUDF "DMS_2_DD"
modUDFs.UnregisterUDF "DD_2_DMS"
modUDFs.UnregisterUDF "ElapsedTime"
End Sub
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
modUDFs.RegisterUDF "Converts Decimal Degrees to Degrees, Minutes, Seconds" & vbLf & "rbcDD_2_DMS(<Decimal Degrees>)", "DD_2_DMS", 3
modUDFs.RegisterUDF "Converts Degrees, Minutes, Seconds to Decimal Degrees" & vbLf & "rbcDMS_2_DD(<Degrees Minutes Seconds>)", "DMS_2_DD", 3
modUDFs.RegisterUDF "Calculates the Time between two time stamps" & vbLf & _
"rbcElapsedTime(<end time>,<start time>,<output code>)" & vbLf & _
"0 = <Seconds>, 1 = <Minutes:Seconds>" & vbLf & _
"2 = <Hours:Minutes:Seconds>, 3 = <Days Hours:Minutes:Seconds>", "ElapsedTime", 2
End Sub
这很有效,似乎解决了我的问题(并且在 99.9% 的时间里都解决了)。但是,了解用户可以随时卸载我的加载项,我想确保在卸载我的加载项时,它提供和注册的 UDF 被取消注册。这意味着@jerryact 提供的解决方案将导致与以前相同的错误消息,但仅在用户打开 Excel 但在实际打开工作簿或创建空白工作簿之前关闭应用程序的特定情况下。这使我得到了我的最终解决方案,如下所示:
Option Explicit
Private WithEvents App As Application
Private Sub App_WindowDeactivate(ByVal Wb As Workbook, ByVal Wn As Window)
modUDFs.UnregisterUDF "DMS_2_DD"
modUDFs.UnregisterUDF "DD_2_DMS"
modUDFs.UnregisterUDF "ElapsedTime"
End Sub
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
modUDFs.RegisterUDF "Converts Decimal Degrees to Degrees, Minutes, Seconds" & vbLf & "rbcDD_2_DMS(<Decimal Degrees>)", "DD_2_DMS", 3
modUDFs.RegisterUDF "Converts Degrees, Minutes, Seconds to Decimal Degrees" & vbLf & "rbcDMS_2_DD(<Degrees Minutes Seconds>)", "DMS_2_DD", 3
modUDFs.RegisterUDF "Calculates the Time between two time stamps" & vbLf & _
"rbcElapsedTime(<end time>,<start time>,<output code>)" & vbLf & _
"0 = <Seconds>, 1 = <Minutes:Seconds>" & vbLf & _
"2 = <Hours:Minutes:Seconds>, 3 = <Days Hours:Minutes:Seconds>", "ElapsedTime", 2
End Sub
请注意,Workbook_BeforeClose 事件已消失,我已将取消注册调用移至 App_WindowDeactivate 事件。这解决了这两个问题,并为我提供了一个可以注册和取消注册我的 UDF 的加载项。上面的所有代码都放在ThisWorkbook 中,下面处理UDF 注册和注销的代码放在我的modUDFs 代码模块中
Sub RegisterUDF(ByVal fDescription As String, ByVal fUDFName As String, ByVal fCategory As Variant)
'Integer Category
'1 Financial
'2 Date & Time
'3 Math & Trig
'4 Statistical
'5 Lookup & Reference
'6 Database
'7 Text
'8 Logical
'9 Information
'10 Commands
'11 Customizing
'12 Macro control
'13 DDE/External
'14 User Defined
'15 First custom category
'16 Second custom category
'17 Third custom category
'18 Fourth custom category
'19 Fifth custom category
'20 Sixth custom category
'21 Seventh custom category
'22 Eighth custom category
'23 Ninth custom category
'24 Tenth custom category
'25 Eleventh custom category
'26 Twelfth custom category
'27 Thirteenth custom category
'28 Fourteenth custom category
'29 Fifteenth custom category
'30 Sixteenth custom category
'31 Seventeenth custom category
'32 Eighteenth custom category
If IsNull(fCategory) Then fCategory = 9
If fCategory = "" Then fCategory = 9
Application.MacroOptions Macro:=fUDFName, Description:=fDescription, Category:=fCategory
End Sub
Sub UnregisterUDF(ByVal fUDFName As String)
Application.MacroOptions Macro:=fUDFName, Description:=Empty, Category:=Empty
End Sub