Call a function from an Add-in to a Macro?

frabulator

Active Member
Joined
Jun 27, 2014
Messages
256
Office Version
  1. 2019
Platform
  1. Windows
I have an add-in (example of 'ADDIN') that is installed in Excel. I have a custom macro that accesses the add-in through VBA to execute subroutines like so (example):

VBA Code:
Dim oApp As Object
Set oApp = Application
Dim Var As Integer
Var = 1
Call oApp.Run("ADDIN.xlam!SUBNAME", Var)

My problem is that I want to run a function from the macro that is contained inside of ADDIN. The function is also accessible through the cell functions once the ADDIN is installed.

My thought was to do something like this, calling it directly, but this did not work saying there needs to be an object.

VBA Code:
Dim Val As Integer
Dim oApp As Object
Set oApp = Application
Dim Var As Integer
Var = 1

Val = Call oApp.Run("ADDIN.xlam!FUNCTIONNAME", Var)

Then my thought was to just call the function through the WorksheetFunction command, but then I realized that the WorksheetFunction command only applies to functions that are default by Excel, not custom.

VBA Code:
Dim Val As Integer
Dim oApp As Object
Set oApp = Application
Dim Var As Integer
Var = 1

Val = WorksheetFunction.FUNCTIONNAME(Var)

I know of a workaround where I can custom code in a subroutine into the ADDIN to change a value of a cell to the function return value, read that value from the MACRO and then change the value back before the screen is updated, but that seems janky and unnecessary.

Does anyone know how I can achieve this more... elegantly?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
VBA Code:
Dim Val As Integer
Dim oApp As Object
Set oApp = Application
Dim Var As Integer
Var = 1

Val = Call oApp.Run("ADDIN.xlam!FUNCTIONNAME", Var)

In the above code, first get rid of the keyword Call. Then, if the actual name of your add-in is different than the one you posted, and if the name contains one or more spaces, you should add single quotes around the filename.

VBA Code:
Val = oApp.Run("'ADDIN.xlam'!FUNCTIONNAME", Var)

Hope this helps!
 
Upvote 1
Solution
In the above code, first get rid of the keyword Call. Then, if the actual name of your add-in is different than the one you posted, and if the name contains one or more spaces, you should add single quotes around the filename.

VBA Code:
Val = oApp.Run("'ADDIN.xlam'!FUNCTIONNAME", Var)

Hope this helps!
Perfect! Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,145
Members
452,615
Latest member
bogeys2birdies

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