简体   繁体   中英

Register user-defined functions in excel addin automatically

I have a macro which registers my user-defined functions, places them in custom categories and supplies descriptions, called AddUDFsToCategory . This macro is placed in a module in an add-in I have installed. However, for the UDFs to become registered, I have to run the macro manually every time I open Excel.

I want this macro to run when I open Excel, even though no workbook is open. I tried to use the Workbook_AddinInstall in the "ThisWorkbook" section of the addin,

Private Sub Workbook_AddinInstall()
    AddUDFsToCategory
End Sub

but that doesn't seem to achieve anything.

If I try to use it with Workbook_open,

Private Sub Workbook_Open()
    AddUDFsToCategory
End Sub

I get:

"Run-time error '1004': Cannot edit a macro on a hidden workbook. Unhide the workbook using the Unhide command."

Does anyone know how to get around this?

You should be able to register it through the Excel Options menu.

First, go to options then click on Add-Ins:

在此处输入图片说明 在此处输入图片说明

From This point, you should be able to manage your add-ins by clicking Go.

在此处输入图片说明


You should also try managing your Add-Ins within the VBE itself.

Open VBE and Click Add-ins :

在此处输入图片说明

Here you will find the Add-ins manager. Select your add-in and ensure that the checkbox that says "Load on Startup" is selected.

The only way I've found to get around this error is to force Excel to have a workbook open when I'm trying to register the UDFs.

Sub AddUDFsToCategory()
   Dim wbkTemp As Excel.Workbook

   If Application.ActiveWorkbook Is Nothing Then
      Set wbkTemp = Application.Workbooks.Add
   End If

   '/--------------------------------------------\
   ' Code to register your User Defined Functions
   '\--------------------------------------------/

   If Not wbkTemp Is Nothing Then
      wbkTemp.Close False
   End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM