Auto run macro on add-in load

DaveBlakeMAAT

Board Regular
Joined
Feb 28, 2016
Messages
190
Hi

I have an excel add-which is running great, however, I wish to start adding my UDFs to it. I have the code to register my UDFs but I was wondering if there is a way of automatically running the UDF registration macro when the add-in is loaded?

Many thanks!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You can call that code from the Workbook_Open procedure of the add-in. That Sub is run when the add-in is loaded.
 
Upvote 0
Hi Jerry

Thank you for taking the time to reply to my post. Unfortunately, I am unable to get your suggestion to work.

I receive the following error:

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

I have the following code in my Add-In workbook object:



Code:
Private Sub Workbook_Open()
    Call AddUDFToCustomCategory
End Sub
Then in my public function module I have:-

Code:
Option Explicit


Public Sub AddUDFToCustomCategory()
    Application.MacroOptions Macro:="InvoiceDueDate", Category:="SDHD", StatusBar:="argument1,argument2,argument3"


End Sub


Public Function InvoiceDueDate(PaymentTerms As String, InvoiceDate As Date, PaymentDays As Integer) As Date
'my function goes here
End Sub


Many thanks

Regards

Dave
 
Upvote 0
Have you tried calling the macro in the Workbook_AddinInstall event instead of the Workbook_Open event ?
 
Upvote 0
Jaafar, Thank you for pointing that out. I haven't used that event and I can see that it's a better fit for an operation like registering UDFs.


Dave, The call would work from the Workbook_Open event however Jaafar's suggestion is better. The error message you described probably occurred if you tried to open an add-in workbook without any other workbooks being open. Try opening a new blank workbook before opening and editing your add-in file.

btw, the code in your OP has an End Sub which should be End Function.

Code:
Public Function InvoiceDueDate(PaymentTerms As String, InvoiceDate As Date, PaymentDays As Integer) As Date
'my function goes here
End [B][COLOR="#FF0000"]Sub[/COLOR][/B]
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top