Download Article
Take advantage of custom functions in Excel with this handy guide
Download Article

Microsoft Excel has many built-in functions, such as SUM, VLOOKUP, and LEFT. As you start using Excel for more complicated tasks, you may find that you need a function that doesn't exist. That's where custom functions come in! This How.com.vn teaches you how to create your own functions in Microsoft Excel.

Things You Should Know

  • Begin creating a user defined function by selecting New Module within the "Insert" menu.
  • Create your function's header by replacing "FunctionName" with a name that outlines the parameters of your function.
  • Be sure to write "EndFunction" at the end of your function's code.
  1. How.com.vn English: Step 1 Open an Excel workbook.
    Double-click the workbook in which you want to use the custom-defined function to open it in Excel.
  2. How.com.vn English: Step 2 Press Alt+F11 (Windows) or Fn+⌥ Opt+F11 (Mac).
    This opens the Visual Basic Editor.
    Advertisement
  3. How.com.vn English: Step 3 Click the Insert menu and select New Module.
    This opens a module window in the right panel of the editor.[1]
    • You can create the user defined function in the worksheet itself without adding a new module, but that will make you unable to use the function in other worksheets of the same workbook.
  4. How.com.vn English: Step 4 Create your function's header.
    The first line is where you will name the function and define our range.[2] Replace "FunctionName" with the name you want to assign your custom function. The function can have as many parameters as you want, and their types can be any of Excel's basic data or object types as Range:
    Function FunctionName (param1 As type1, param2 As type2 ) As return Type

    • You may think of parameters as the "operands" your function will act upon. For example, when you use SIN(45) to calculate the Sine of 45 degree, 45 will be taken as a parameter. Then the code of your function will use that value to calculate something else and present the result.
  5. How.com.vn English: Step 5 Add the code of the function.
    Make sure you use the values provided by the parameters, assign the result to the name of the function, and close the function with "End Function." Learning to program in VBA or in any other language can take some time and a detailed tutorial. However, functions usually have small code blocks and use very few features of the language. Some useful elements are:
    • The If block, which allows you to execute a part of the code only if a condition is met. Notice the elements in an If code block: IF condition THEN code ELSE code END IF. The Else keyword along with the second part of the code are optional:
      Function Course Result(grade As Integer) As String  If grade >= 5 Then    CourseResult = "Approved"  Else    CourseResult = "Rejected"  End IfEnd Function

    • The Do block, which executes a part of the code While or Until a condition is met. In the example code below, notice the elements DO code LOOP WHILE/UNTIL condition. Also notice the second line in which a variable is declared. You can add variables to your code so you can use them later. Variables act as temporary values inside the code. Finally, notice the declaration of the function as BOOLEAN, which is a datatype that allows only the TRUE and FALSE values. This method of determining if a number is prime is by far not the optimal, but I've left it that way to make the code easier to read.
      Function IsPrime(value As Integer) As Boolean  Dim i As Integer  i = 2  IsPrime = True  Do    If value / i = Int(value / i) Then      IsPrime = False    End If    i = i + 1  Loop While i < value And IsPrime = TrueEnd Function
    • The For block executes a part of the code a specified number of times. In this next example, you'll see the elements FOR variable = lower limit TO upper limit code NEXT. You'll also see the added ElseIf element in the If statement, which allows you to add more options to the code that is to be executed. Additionally, the declaration of the function and the variable result as Long. The Long datatype allows values much larger than Integer:
      Public Function Factorial(value As Integer) As Long  Dim result As Long  Dim i As Integer  If value = 0 Then    result = 1  ElseIf value = 1 Then    result = 1  Else    result = 1    For i = 1 To value      result = result * i    Next  End If  Factorial = resultEnd Function
  6. How.com.vn English: Step 6 Close the Visual Basic Editor.
    Once you've created your function, close the window to return to your workbook. Now you can start using your user-defined function.
  7. How.com.vn English: Step 7 Enter your function.
    First, click the cell in which you want to enter the function. Then, click the function bar at the top of Excel (the one with the fx to its left) and type =FUNCTIONNAME(), replacing FUNCTIONNAME with the name you assigned your custom function.
    • You can also find your user-defined formula in the "User Defined" category in the Insert Formula wizard—just click the fx to pull up the wizard.
  8. How.com.vn English: Step 8 Enter the parameters into the parentheses.
    For example, =NumberToLetters(A4). The parameters can be of three types:
    • Constant values typed directly in the cell formula. Strings have to be quoted in this case.
    • Cell references like B6 or range references like A1:C3. The parameter has to be of the Range datatype.
    • Other functions nested inside your function. Your function can also be nested inside other functions. Example: =Factorial(MAX(D6:D8)).
  9. How.com.vn English: Step 9 Press ↵ Enter or ⏎ Return to run the function.
    The results will display in the selected cell.
  10. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How can I use these functions in all Excel files?
    How.com.vn English: Igal Livne
    Igal Livne
    Community Answer
    Save the workbook with the custom class as "Excel Add-In (*.xlam")," by default Excel will take you to "Addins" folder. Go to Excel Options > Add-ins > Manage: "Excel Addins" - press "Go..." button. Browse for your newly create xlam file.
  • Question
    How can I do well in exams?
    How.com.vn English: Community Answer
    Community Answer
    Read the directions several times, leaving time for you to absorb between readings. Also practice writing VBA to do various things.
  • Question
    How do I know what to write as the function code?
    How.com.vn English: Community Answer
    Community Answer
    In order to create functions, you need a skill called "programming". Excel macros are written in a language called "Visual Basic for Applications", which you will need to learn to be able to write macros. It's quite easy once you've got the hang of it though!
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      Tips

      • Use a name that's not already defined as a function name in Excel or you'll end up being able to use only one of the functions.
      • Whenever you write a block of code inside a control structure like If, For, Do, etc. make sure you indent the block of code using a few blank spaces or the Tab key. That will make your code easier to understand and you'll find a lot easier to spot errors and make improvements.
      • If you don't know how to write the code for a function, see How to Write a Simple Macro in Microsoft Excel.
      Show More Tips
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      Warnings

      • The functions used in this article are, by no means, the best way to solve the related problems. They were used here only to explain the usage of the control structures of the language.
      • VBA, as any other language, has several other control structures besides Do, If and For. Those have been explained here only to clarify what kind of things can be done inside the function source code. There are many online tutorials available where you can learn VBA.
      • Due to security measures, some people may disable macros. Make sure you let your colleagues know the book you're sending them has macros and that they can trust they're not going to damage their computers.
      Advertisement

      About This Article

      How.com.vn English: Nicole Levine, MFA
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for How.com.vn. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 650,912 times.
      How helpful is this?
      Co-authors: 15
      Updated: May 1, 2023
      Views: 650,912
      Categories: Microsoft Excel
      Article SummaryX

      1.Open Excel.
      2.Press Alt + F11 to open the Visual Basic editor.
      3.Create the function's name and set the parameter types.
      4.Use VB code to write your function.
      5.Close the editor.
      6.Run your function as you would other functions.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 650,912 times.

      Reader Success Stories

      • How.com.vn English: G. Weiss

        G. Weiss

        Jan 31, 2017

        "Well done with lots of detail."
      Share your story

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement