SOLVED

Adding new Excel Functions

%3CLINGO-SUB%20id%3D%22lingo-sub-2640621%22%20slang%3D%22en-US%22%3EAdding%20new%20Excel%20Functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2640621%22%20slang%3D%22en-US%22%3E%3CP%3EWindows%2010%2064%20bit%20v21H1.%26nbsp%3B%20Office%20365%20Excel%20v2107%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20add%20additional%20functions%20to%20my%20worksheets.%26nbsp%3B%20Online%20postings%20(many%20of%20which%20relate%20to%20earlier%20sw%20versions%20and%20are%20out%20of%20date)%20have%20got%20me%20as%20far%20as%20defining%20a%20function%20in%20VBA%20but%20I%20cannot%20seem%20to%20get%20this%20to%20work%20in%20the%20associated%20worksheet%20or%20elsewhere.%26nbsp%3B%20How%20am%20I%20meant%20to%20save%20my%20code%20so%20that%20I%20can%20get%20it%20to%20work%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2640621%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2641157%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20new%20Excel%20Functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2641157%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1126907%22%20target%3D%22_blank%22%3E%40Dicky_g141%3C%2FA%3E%26nbsp%3BWhat%20type%20of%20calculation%20are%20you%20trying%20to%20do%20using%20a%20VBA%20function%3F%20Please%20be%20aware%2C%20that%20VBA%20functions%20you%20call%20from%20cells%20will%20be%20slooooooow.%20Chances%20are%20you%20can%20solve%20your%20problem%20using%20a%20combination%20of%20existing%20functions.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2641196%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20new%20Excel%20Functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2641196%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1126907%22%20target%3D%22_blank%22%3E%40Dicky_g141%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20the%20function%20is%20intended%20for%20use%20in%20a%20specific%20workbook%2C%20make%20sure%20that%20you%20create%20the%20code%20for%20the%20function%20in%20a%20standard%20module%20in%20that%20workbook.%20A%20standard%20module%20is%20the%20type%20of%20module%20that%20you%20create%20by%20selecting%20Insert%20%26gt%3B%20Module%20in%20the%20Visual%20Basic%20Editor.%3C%2FP%3E%0A%3CP%3EIt%20won't%20work%20if%20you%20create%20the%20code%20in%20the%20code%20module%20of%20a%20worksheet%20or%20in%20the%20ThisWorkbook%20module.%3C%2FP%3E%0A%3CP%3EMake%20sure%20that%20you%20save%20the%20workbook%20as%20a%20macro-enabled%20workbook%20(*.xlsm%20or%20*.xlsb)%3B%20if%20you%20save%20it%20as%20a%20.xlsx%20workbook%2C%20all%20VBA%20code%20will%20be%20lost.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20the%20function%20should%20be%20available%20in%20all%20open%20workbooks%2C%20you%20must%20create%20it%20in%20your%20personal%20macro%20workbook%20PERSONAL.XLSB.%20If%20you%20don't%20have%20this%20yet%2C%20you%20must%20create%20it%20by%20recording%20a%20macro%20in%20your%20Personal%20Macro%20Workbook%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22S0661.png%22%20style%3D%22width%3A%20344px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F302743i2301E0674C91AF31%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22S0661.png%22%20alt%3D%22S0661.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAfter%20that%2C%20you%20can%20remove%20the%20dummy%20macro%20that%20you%20recorded%2C%20and%20create%20your%20own%20functions.%3C%2FP%3E%0A%3CP%3ETo%20refer%20to%20a%20function%20in%20your%20personal%20macro%20workbook%20in%20a%20formula%2C%20prefix%20it%20with%20PERSONAL.XLSB.%20For%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3DPERSONAL.XLSB!MyFunction()%3C%2FP%3E%0A%3CP%3Eor%3C%2FP%3E%0A%3CP%3E%3DPERSONAL.XLSB!MyFunction(A1%2CA2)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20you%20quit%20Excel%2C%20it%20will%20prompt%20you%20to%20save%20changes%20in%20PERSONAL.XLSB.%20Answer%20Yes!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2641375%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20new%20Excel%20Functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2641375%22%20slang%3D%22en-US%22%3EI%20am%20just%20experimenting%20with%20an%20interpolation%20function%20but%20there%20are%20a%20number%20of%20others%20I%20plan%20to%20write%20when%20I've%20got%20this%20one%20working.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2644806%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20new%20Excel%20Functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2644806%22%20slang%3D%22en-US%22%3EMy%20warning%20about%20performance%20does%20not%20go%20away%20I'm%20afraid%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%20Have%20you%20considered%20the%20new%20LAMBDA%20functions%3F%20%3CA%20href%3D%22https%3A%2F%2Fjkp-ads.com%2FArticles%2FExcel-lambda-function-convert-vba.asp%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fjkp-ads.com%2FArticles%2FExcel-lambda-function-convert-vba.asp%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2645646%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20new%20Excel%20Functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2645646%22%20slang%3D%22en-US%22%3EThank%20you%20Jan%20but%20I%20think%20I'll%20stick%20with%20VBA%20until%20I've%20got%20that%20mastered%20and%20then%20look%20elsewhere%20if%20it%20isn't%20performing%20as%20I%20want.%3C%2FLINGO-BODY%3E
Occasional Contributor

Windows 10 64 bit v21H1.  Office 365 Excel v2107

 

I am trying to add additional functions to my worksheets.  Online postings (many of which relate to earlier sw versions and are out of date) have got me as far as defining a function in VBA but I cannot seem to get this to work in the associated worksheet or elsewhere.  How am I meant to save my code so that I can get it to work?

13 Replies

@Dicky_g141 What type of calculation are you trying to do using a VBA function? Please be aware, that VBA functions you call from cells will be slooooooow. Chances are you can solve your problem using a combination of existing functions.

best response confirmed by Dicky_g141 (Occasional Contributor)
Solution

@Dicky_g141 

If the function is intended for use in a specific workbook, make sure that you create the code for the function in a standard module in that workbook. A standard module is the type of module that you create by selecting Insert > Module in the Visual Basic Editor.

It won't work if you create the code in the code module of a worksheet or in the ThisWorkbook module.

Make sure that you save the workbook as a macro-enabled workbook (*.xlsm or *.xlsb); if you save it as a .xlsx workbook, all VBA code will be lost.

 

If the function should be available in all open workbooks, you must create it in your personal macro workbook PERSONAL.XLSB. If you don't have this yet, you must create it by recording a macro in your Personal Macro Workbook:

S0661.png

After that, you can remove the dummy macro that you recorded, and create your own functions.

To refer to a function in your personal macro workbook in a formula, prefix it with PERSONAL.XLSB. For example:

 

=PERSONAL.XLSB!MyFunction()

or

=PERSONAL.XLSB!MyFunction(A1,A2)

 

When you quit Excel, it will prompt you to save changes in PERSONAL.XLSB. Answer Yes!

I am just experimenting with an interpolation function but there are a number of others I plan to write when I've got this one working.
My warning about performance does not go away I'm afraid Have you considered the new LAMBDA functions? https://jkp-ads.com/Articles/Excel-lambda-function-convert-vba.asp

@Hans Vogelaar 

Thank you Hans.  That seems very helpful but I still seem to be missing something basic.  I have entered the following in a module edit window, as you suggest:

Function Interpolate(lwr, upr, lwrval, uprval, x)
Interpolate = lwrval + (x - lwr) * (uprval - lwrval) / (upr - lwr)
End Function

I have also tried adding "As single" to the first line and both variants compile successfully but neither seems to propagate into my worksheet, which refuses to admit to the existence of a function with the name Interpolate.  What am I missing?

Thank you Jan but I think I'll stick with VBA until I've got that mastered and then look elsewhere if it isn't performing as I want.
Thanks again, Hans, but it appears that the only problem was with the name "INTERPOLATE". It didn't like the mixed-case version and appears to think that the uppercase version is already in use (although it doesn't appear in the all functions list). Renaming it "FOOBAR" seems to have fixed it.
For anyone who is interested in this thread I have found that, by saving my macros in an Excel add-in file (.xalm extension) it propagates into all spreadsheets and the macros can just be used as is, that is without the MYMACRO! prefix.
Note that this isn't true for sharing workbooks using your UDF with a colleague; the location of your UDF is stored in your workbook and if that location is different for your colleague (usually the case!) then the UDF will not work. Hence: https://jkp-ads.com/Articles/FixLinks2UDF.asp
Thanks for the warning, Jan. I have no immediate plans to share these functions but, if I do, I shall invite recipients to re-create them for themselves.
As an aside I wanted to award "likes" to both you and Hans for your helpful contributions. However, while I can apparently view likes awarded by others (not that there are any in this thread), there seems to be no way to award them myself.

@Dicky_g141 

Can't you click on the little thumb-up icon?

Hello Hans. Indeed I can but that just takes me to a view of who has previously liked the posting. It does not allow me to add a like.
Aha! A bit of finger trouble there. It seems that I have been clicking on "Likes" rather than the thumb! Problem sorted thank you.