PLEASE VIEW TO UPDATE BECAUSE I OPEN IN THIS STATEMENT WHAT I DO DO
In conclusion, you will have to put your function in the class in your appendix, but there is an additional step for creating cross-workbook scripts; you cannot use the New keyword for the outer class. Therefore, you need to write a function of the factory class that can be called from the outside.
The next problem is the link, you can use the links to the tools and link to the project to get an early link to its useful Intellisense, but you potentially create a rod for your back due to the loading sequence, addin will be loaded by any client call that has the link. An alternative is the Late Bound equivalent, which removes the link, but puts the burden on downloading the add-on on the developer.
Here are the steps ...
Create a project, I named my FunctionLibrary.xlsm, I renamed the Project Property from 'VBAProject' to FunctionLibrary.
Add a class to your project, I called MyLibrary, I set Instancing to 2 - PublicNotCreateable . I added the (simple) following code
Option Explicit Public Function Add(x, y) Add = x + y End Function
- Add a standard module called 'modEarlyBoundClassFactory' and add the following code
Option Explicit Public Function CreateMyLibraryEarlyBoundEntryPoint(ByVal sLicenceKey As String) As MyLibrary 'If sLicenceKey = "Yourlicencekey" Then Set CreateMyLibraryEarlyBoundEntryPoint = New MyLibrary 'End If End Function
- In the ThisWorkbook module, I added the following code
Option Explicit Public Function CreateMyLibraryLateBoundEntryPoint(ByVal sLicenceKey As String) As Object 'If sLicenceKey = "Yourlicencekey" Then Set CreateMyLibraryLateBoundEntryPoint = New MyLibrary 'End If End Function
- Save book
- Create a calling book, I called my FunctionLibraryCallers.xlsm and in the new standard module I added the following code
Option Explicit
Sub EarlyBoundTest() '* requires Tools->References to addin (this can create load sequence issues and the addin equivalent of dll hell)! Dim o As FunctionLibrary.mylibrary Set o = FunctionLibrary.CreateMyLibraryEarlyBoundEntryPoint("open sesame") Debug.Print o.Add(4, 5)
End Sub
Sub LateBoundTest()
'* you need to write code to ensure the function library is loaded!!! On Error Resume Next Dim wbFL As Excel.Workbook Set wbFL = Application.Workbooks.Item("FunctionLibrary.xlsm") On Error GoTo 0 Debug.Assert Not wbFL Is Nothing '* End of 'you need to write code to ensure the function library is loaded!!!' Dim o As Object 'FunctionLibrary.mylibrary Set o = wbFL.CreateMyLibraryLateBoundEntryPoint("open sesame") '* this works because the method is defined in ThisWorkbook module of library Debug.Print o.Add(4, 5)
Sub
code>
- To start the top unit, you will need to go to Tools-> Links and Links FunctionLibrary.xlsm.
- To run the bottom section, no tools are needed-> Link, although you will need to comment on the top to avoid compilation errors.
UPDATE: Folding feedback with a comment. The Hell DLL is when you move the code to the library, and then you have to worry about loading it, loading the correct version and the correct dependencies.
ThisWorkbook
OP asks about ThisWorkbook, this idea came from another SO> question about compilation errors. if you define the variable as a workbook, the compiler will not use the standard workbook interface. One of them has the right to name additional methods not found in the standard interface. I suggested that this was because ThisWorkbook could be used as an extensibility mechanism.
ThisWorkbook hides functions from the Insert Function dialog box.
Interestingly, ThisWorkbook hides a function from the Insert Function dialog box, so this is an easier way to achieve OP requirements!
ThisWorkbook hides features and subtitles from Application.Run
Actually, since ThisWorkbook is one instance of the class, then all the functions and sub-settings defined by the developer are not added to the global namespace inside it, so Application.Run cannot be called on them. To execute them, you need to get a link to the Excel.Workbook object for the library tutorial and call methods through this instance.
Yes, this works for both xlam and xlsm.
Thanks to OP, today I learned something.