How to Create Custom Excel Functions. User Defined Function (UDF) Examples f

download How to Create Custom Excel Functions. User Defined Function (UDF) Examples f

of 4

Transcript of How to Create Custom Excel Functions. User Defined Function (UDF) Examples f

  • 8/10/2019 How to Create Custom Excel Functions. User Defined Function (UDF) Examples f

    1/4

    ow to Create Custom Excel Functions. User Defined Function (UDF) Examples for Excel.

    " THE Guide to Excel in Everything "

    Home | Excel Links | Excel Templates SEARCH:

    How to Create Custom User Defined ExcelFunctions

    Advanced Excel Techniques - (1/7/04)- Jon WittwerVertex42, LLC

    Excel allows you to create custom functions, called "User Defined Functions" (UDF's) that can bused the same way you would use SUM() or some other built-in Excel function.

    The Excel enthusiast who wishes to use advanced mathematicsor perform text manipulationoften seriously disappointed by Excel's limited library of formulas and functions. However, all is no

    For an excellent explanation of pretty much everything you need to know to create your own custoExcel function, I would recommend John Walkenbach's book, Excel 200X Formulas. The book

    provides many good user defined function examples, so if you like to learn by example, it is a

    greatresource.

    This article will help you get started with user defined functionsand show a couple of coolexamples. If you are a software developer, specifically for engineering applications, I wouldrecommend the following course:

    Microsoft Excel VBA User Defined Function Design for Engineersby EMAGENIT.com

    "... The ability to make UDF's allows complex engineering calculations to take place in a single Microsoft Excel worksheetVBA UDF's can also be packaged into procedure libraries and distributed. This allows standardized rapid analysis environmbe developed cheaply in Microsoft Excel. They can even communicate with FORTRAN and C utilizing their source code." L

    More ...

    How to Create Excel User Defined Functions

    1. Open up a new workbook.2. Get into VBA (Press Alt+F11)3. Insert a new module (Insert > Module)4. - Copy and Paste the Excel user defined function examples -5. Get out of VBA (Press Alt+Q)6. Use the functions (They will appear in the Paste Function dialog box, Shift+F3, under the "

    Defined" category)

    If you want to use a UDF in more than one workbook, you can save your functions in your own cuadd-in. Simply save your excel file that contains your VBA functions as an add-in file (.xla). Then the add-in (Tools > Add-Ins...). Warning!Be careful about using custom functions in spreadsheethat you need to share with others. If they don't have your add-in, the functions will not work whe

    they use the spreadsheet.

    Benefits of User Defined Excel Functions

    Create a complex or custom mathfunction. Simplify formulas that would otherwise be extremely long "mega formulas". Diagnosticssuch as checking cell formats. Custom text manipulation. Advanced array formulasand matrix functions.

    Limitations of UDF's

    Quick Links ...

    ate Excel Functions

    efits of Excel UDFs

    tationsExamples

    onal UDF Argument

    ertex42 Articles

    ortization Formulas

    shboard Reports

    ng Excel Solver

    ancial Modeling

    bt Consolidation

    nte Carlo Simulation

    istograms in Excel

    andom Numbers

    ormal Distribution

    aph

    cel Web Queries

    stom Excel Functions

    cel Training

    olbar Buttons

    nificant Figures

    n and Games

    xcel Templatesmortization Schedule

    sset Tracking

    ttendance Tracking

    alloon Loan

    xcel Calendar

    heckbook

    ass Schedule

    xpense Reports

    antt Chart

    nventory

    nvoice

    ortgage

    ersonal Budget

    roject Management

    urchase Order

    etirement Savings

    tock Quotes

    mesheet

    meline Examples

    Work Schedule

    ttp://www.vertex42.com/ExcelArticles/user-defined-functions.html (1 of 4) [1/5/2007 8:50:02 PM]

    G

    http://www.vertex42.com/http://www.vertex42.com/ExcelLinks/http://www.vertex42.com/ExcelTemplates/http://www.amazon.com/exec/obidos/ASIN/076454800X/vertex42-20http://www.emagenit.com/excelvbaudf.htmhttp://www.emagenit.com/excelvbaudf.htmhttp://www.emagenit.com/excelvbaudf.htmhttp://www.emagenit.com/excelvbaudf.htmhttp://www.vertex42.com/ExcelArticles/amortization-formulas.htmlhttp://www.vertex42.com/ExcelArticles/excel-dashboards.htmlhttp://www.vertex42.com/ExcelArticles/excel-solver-examples.htmlhttp://www.vertex42.com/ExcelArticles/financial-modeling.htmlhttp://www.vertex42.com/ExcelArticles/debt-consolidation.htmlhttp://www.vertex42.com/ExcelArticles/mc/index.htmlhttp://www.vertex42.com/ExcelArticles/mc/Histogram.htmlhttp://www.vertex42.com/ExcelArticles/mc/GeneratingRandomInputs.htmlhttp://www.vertex42.com/ExcelArticles/mc/NormalDistribution-Excel.htmlhttp://www.vertex42.com/ExcelArticles/mc/NormalDistribution-Excel.htmlhttp://www.vertex42.com/News/excel-web-query.htmlhttp://www.vertex42.com/ExcelArticles/free-excel-training.htmlhttp://www.vertex42.com/ExcelTips/excel-toolbar-buttons.htmlhttp://www.vertex42.com/ExcelTips/significant-figures.htmlhttp://www.vertex42.com/ExcelArticles/fun-with-excel.htmlhttp://www.vertex42.com/ExcelTemplates/excel-amortization-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/asset-tracking-software.htmlhttp://www.vertex42.com/ExcelTemplates/attendance-tracking.htmlhttp://www.vertex42.com/ExcelTemplates/balloon-loan-calculator.htmlhttp://www.vertex42.com/ExcelTemplates/excel-calendar-template.htmlhttp://www.vertex42.com/ExcelTemplates/excel-checkbook.htmlhttp://www.vertex42.com/ExcelTemplates/class-schedule-template.htmlhttp://www.vertex42.com/ExcelTemplates/excel-expense-report.htmlhttp://www.vertex42.com/ExcelTemplates/excel-gantt-chart.htmlhttp://www.vertex42.com/ExcelTemplates/inventory-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/excel-invoice-template.htmlhttp://www.vertex42.com/ExcelTemplates/mortgage-interest-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/personal-budget-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/excel-project-management.htmlhttp://www.vertex42.com/ExcelTemplates/excel-purchase-order.htmlhttp://www.vertex42.com/ExcelTemplates/retirement-planning-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/excel-stock-quotes.htmlhttp://www.vertex42.com/ExcelTemplates/excel-timesheet.htmlhttp://www.vertex42.com/ExcelTemplates/timeline.htmlhttp://www.vertex42.com/ExcelTemplates/work-schedule-template.htmlhttp://www.vertex42.com/ExcelTemplates/work-schedule-template.htmlhttp://www.vertex42.com/ExcelTemplates/timeline.htmlhttp://www.vertex42.com/ExcelTemplates/excel-timesheet.htmlhttp://www.vertex42.com/ExcelTemplates/excel-stock-quotes.htmlhttp://www.vertex42.com/ExcelTemplates/retirement-planning-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/excel-purchase-order.htmlhttp://www.vertex42.com/ExcelTemplates/excel-project-management.htmlhttp://www.vertex42.com/ExcelTemplates/personal-budget-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/mortgage-interest-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/excel-invoice-template.htmlhttp://www.vertex42.com/ExcelTemplates/inventory-spreadsheet.htmlhttp://www.vertex42.com/ExcelTemplates/excel-gantt-chart.htmlhttp://www.vertex42.com/ExcelTemplates/excel-expense-report.htmlhttp://www.vertex42.com/ExcelTemplates/class-schedule-template.htmlhttp://www.vertex42.com/ExcelTemplates/excel-checkbook.htmlhttp://www.vertex42.com/ExcelTemplates/excel-calendar-template.htmlhttp://www.vertex42.com/ExcelTemplates/balloon-loan-calculator.htmlhttp://www.vertex42.com/ExcelTemplates/attendance-tracking.htmlhttp://www.vertex42.com/ExcelTemplates/asset-tracking-software.htmlhttp://www.vertex42.com/ExcelTemplates/excel-amortization-spreadsheet.htmlhttp://www.vertex42.com/ExcelArticles/fun-with-excel.htmlhttp://www.vertex42.com/ExcelTips/significant-figures.htmlhttp://www.vertex42.com/ExcelTips/excel-toolbar-buttons.htmlhttp://www.vertex42.com/ExcelArticles/free-excel-training.htmlhttp://www.vertex42.com/News/excel-web-query.htmlhttp://www.vertex42.com/ExcelArticles/mc/NormalDistribution-Excel.htmlhttp://www.vertex42.com/ExcelArticles/mc/NormalDistribution-Excel.htmlhttp://www.vertex42.com/ExcelArticles/mc/GeneratingRandomInputs.htmlhttp://www.vertex42.com/ExcelArticles/mc/Histogram.htmlhttp://www.vertex42.com/ExcelArticles/mc/index.htmlhttp://www.vertex42.com/ExcelArticles/debt-consolidation.htmlhttp://www.vertex42.com/ExcelArticles/financial-modeling.htmlhttp://www.vertex42.com/ExcelArticles/excel-solver-examples.htmlhttp://www.vertex42.com/ExcelArticles/excel-dashboards.htmlhttp://www.vertex42.com/ExcelArticles/amortization-formulas.htmlhttp://www.emagenit.com/excelvbaudf.htmhttp://www.emagenit.com/excelvbaudf.htmhttp://www.emagenit.com/excelvbaudf.htmhttp://www.amazon.com/exec/obidos/ASIN/076454800X/vertex42-20http://www.vertex42.com/ExcelTemplates/http://www.vertex42.com/ExcelTemplates/http://www.vertex42.com/ExcelLinks/http://www.vertex42.com/ExcelLinks/http://www.vertex42.com/
  • 8/10/2019 How to Create Custom Excel Functions. User Defined Function (UDF) Examples f

    2/4

    ow to Create Custom Excel Functions. User Defined Function (UDF) Examples for Excel.

    Cannot "record" an Excel UDFlike you can an Excel macro. More limited than regular VBA macros. UDF's cannot alter the structure or formatof a

    worksheet or cell. If you call another function or macro from a UDF, the other macro is under the same limitation

    the UDF. Cannot place a value in a cell other than the cell (or range) containing the formula. In other wo

    UDF's are meant to be used as "formulas", not necessarily "macros". Excel user defined functions in VBA are usually much slowerthan functions compiled in C++

    FORTRAN. Often difficult to track errors. If you create an add-in containing your UDF's, you may forget that you have used a custom

    function, making the file less sharable. Adding user defined functions to your workbook will trigger the "macro" flag(a security issu

    Tools > Macros > Security...).

    User Defined Function Examples

    Example #1: Get the Address of a Hyperlink

    The following example can be useful when extracting hyperlinks from tables of links that habeen copied into Excel, when doing post-processing on Excel web queries, or getting the emaddress from a list of "mailto:" hyperlinks.

    This function is also an example of how to use an optional Excel UDF argument. The synfor this custom Excel function is:

    =LinkAddress(c e l l ,[d e f a u l t _ v a l u e ])

    To see an example of how to work with optional arguments, look up the IsMissingcommin Excel's VBA help files (F1).

    FunctionLinkAddress(cell Asrange, _Optional default_value AsVariant)

    'Lists the Hyperlink Address for a Given Cell

    'If cell does not contain a hyperlink, return default_valueIf(cell.range("A1").Hyperlinks.Count 1) Then

    LinkAddress = default_valueElse

    LinkAddress = cell.range("A1").Hyperlinks(1).AddressEndIf

    EndFunction

    Example #2: Extract the Nth Element From a String

    This example shows how to take advantage of some functions available in VBA in order to dsome slick text manipulation. What if you had a bunch of telephone numbers in the followin

    format: 1-800-999-9999and you wanted to pull outjustthe 3-digit prefix?

    This UDF takes as arguments the textstring, the number of the element you want to grab and the delimiter as a string (eg. "-"). The syntax for this example user defined function in is:

    =GetElement(t e x t ,n,del i mi t e r)

    Example:If B3 contains "1-800-333-4444", and cell C3 contains the formula, =GetElemen(B3,3,"-"), C3 will then equal "333". To turn the "333" into a number, you would use =VALU(GetElement(B3,3,"-")).

    early Calendar

    Links ...

    ttp://www.vertex42.com/ExcelArticles/user-defined-functions.html (2 of 4) [1/5/2007 8:50:02 PM]

    http://www.vertex42.com/ExcelTemplates/yearly-calendar.htmlhttp://www.vertex42.com/ExcelLinks/category.php?n=1http://rcm.amazon.com/e/cm/privacy-policy.html?o=1http://www.amazon.com/exec/obidos/redirect-home/vertex42-20http://rcm.amazon.com/e/cm/privacy-policy.html?o=1http://www.amazon.com/exec/obidos/redirect-home/vertex42-20http://www.vertex42.com/ExcelLinks/category.php?n=1http://www.vertex42.com/ExcelTemplates/yearly-calendar.html
  • 8/10/2019 How to Create Custom Excel Functions. User Defined Function (UDF) Examples f

    3/4

    ow to Create Custom Excel Functions. User Defined Function (UDF) Examples for Excel.

    FunctionGetElement(text AsVariant, n AsInteger, _delimiter AsString) AsString

    'Returns the nth element from a delimited text stringDimtxt, str AsString

    Dimcount, i AsInteger

    'Manipulate a copy of the text stringtxt = text

    'If a space is used as the delimiter, remove extra spaces

    Ifdelimiter = Chr(32) Thentxt = Application.Trim(txt)

    'Add a delimiter to the end of the stringIfRight(txt, Len(txt)) delimiter Then

    txt = txt & delimiterEndIf

    'Initialize count and elementcount = 0str = ""

    'Get each element

    Fori = 1 To Len(txt)IfMid(txt, i, 1) = delimiter Then

    count = count + 1Ifcount = n Then

    GetElement = strExitFunction

    Else

    str = ""EndIf

    Elsestr = str & Mid(txt, i, 1)

    EndIfNexti

    GetElement = ""EndFunction

    Example #3: UDF for a Custom Mathematical Formula

    One of the nice things about custom Excel functions is that you can simplify Excel formulas would otherwise use nested If...Then...statements. As an example, let's say we have a simfunction that includes division, but the formula changes when the divisor is zero. Also, want to do some error checking, so we don't end up with #VALUEall over our spreadsheet

    For this example, we'll look at the KEI formula (Keyword Effectiveness Index), which whensimplified looks something like this when using built-in Excel functions:

    =IF(supply=0,demand 2,demand 2/supply)

    The syntax for the custom user defined function is:

    =KEI(de man d,s u p p l y ,[d e f a u l t _ v a l u e ])

    FunctionKEI(demand AsVariant, supply AsVariant, _

    Optional default_value AsVariant) AsVariant'Keyword Effectiveness Index (KEI)

    IfIsMissing(default_value) Then

    ttp://www.vertex42.com/ExcelArticles/user-defined-functions.html (3 of 4) [1/5/2007 8:50:02 PM]

  • 8/10/2019 How to Create Custom Excel Functions. User Defined Function (UDF) Examples f

    4/4

    ow to Create Custom Excel Functions. User Defined Function (UDF) Examples for Excel.

    default_value = "n/a"EndIfIfIsNumeric(demand) AndIsNumeric(supply) Then

    Ifsupply = 0 Then

    KEI = demand ^ 2ExitFunction

    ElseKEI = demand ^ 2 / supplyExitFunction

    EndIfEndIfKEI = default_value

    EndFunction

    More Custom Excel Function Examples

    Rounding Significant Figures in Excel:: Shows how to return #NUM and #N/A error values

    There are certainly many more examples of UDF's for Excel. Again, I would strongly recommenJohn Walkenbach's book, Excel 200X Formulas. But, if you are looking for examples on the

    internet, you may want to check out the following websites:

    User Defined Function Examples- www.ozgrid.com

    Examples of user defined functionsor UDF's for Excel written in VBA. Random numbers, Hyperlinks, count sum or socolors.

    UDF Examples and Tips- www.exceltip.com

    Writing Your First VBA Function in Excel. Area of a rectangle (perhaps a little too simple), fuel consumption, and other infUDF's.

    Build an Excel Add-In- http://www.fontstuff.com/vba/vbatut03.htm

    An excellent tutorial that takes you through building an add-in for a custom excel function.

    CITE THIS ARTICLE AS:

    Wittwer, J.W., "How to Create Custom User Defined Excel Functions"From Vertex42.com, January, 2004, http://www

    vertex42.com/ExcelArticles/user-defined-functions.html

    [ Home | Sitemap | Support | About/Contact Us | Privacy | Legal Stuff ]

    003 Vertex42, LLC All rights reserved. Custom Excel Function

    ttp://www.vertex42.com/ExcelArticles/user-defined-functions.html (4 of 4) [1/5/2007 8:50:02 PM]

    http://www.vertex42.com/ExcelTips/significant-figures.htmlhttp://www.amazon.com/exec/obidos/ASIN/076454800X/vertex42-20http://www.ozgrid.com/VBA/Functions.htmhttp://www.exceltip.com/show_tip/Excel_Custom_Functions_using_VBA/Writing_Your_First_VBA_Function_in_Excel/631.htmlhttp://www.fontstuff.com/vba/vbatut03.htmhttp://www.vertex42.com/http://www.vertex42.com/index.htmlhttp://www.vertex42.com/sitemap.htmlhttp://www.vertex42.com/support.htmlhttp://www.vertex42.com/about.htmlhttp://www.vertex42.com/privacy.htmlhttp://www.vertex42.com/terms.htmlhttp://www.vertex42.com/terms.htmlhttp://www.vertex42.com/privacy.htmlhttp://www.vertex42.com/about.htmlhttp://www.vertex42.com/support.htmlhttp://www.vertex42.com/sitemap.htmlhttp://www.vertex42.com/index.htmlhttp://www.vertex42.com/http://www.fontstuff.com/vba/vbatut03.htmhttp://www.exceltip.com/show_tip/Excel_Custom_Functions_using_VBA/Writing_Your_First_VBA_Function_in_Excel/631.htmlhttp://www.ozgrid.com/VBA/Functions.htmhttp://www.amazon.com/exec/obidos/ASIN/076454800X/vertex42-20http://www.vertex42.com/ExcelTips/significant-figures.html