TechRepublic : A ZDNet Tech Community

How do I… Create a user-defined function in Microsoft Excel

Tags: Sales force management, Programming languages, Mark Kaelin, Microsoft Corp., user-defined function, commission, Microsoft Excel

  • Save
  • Print
  • Recommend
  • 10

Takeaway: Microsoft Excel allows users to create their own user-defined functions. Using the power of Visual Basic for Applications (VBA), you can code complicated formulas only once and use them again and again in your spreadsheets and workbooks. These functions can be used just like any built-in function already available in Excel. Here's how you create one.

Sometimes you find yourself in a situation where you can't get Microsoft Excel to do exactly what you want it to do without coding a complicated formula over and over again. In such a scenario, you often have to spend an unacceptable amount of time finding circular logic errors or fixing syntax problems. Using the power of Visual Basic for Applications (VBA), you can code that complicated formula only once and use it again and again in a user-defined function.



Click this tag search to find other How do I… articles and downloads.




Build your function

According to Microsoft, to create a user-defined function in Excel, you first call up the Visual Basic Editor, which is located in the Tools | Macros menu. Once in the editor, navigate to Insert | Module to open a blank module screen. This is where you'll type in your code. (See Figure A.)

Figure A

Visual Basic Editor

For example, let's assume you want to calculate the commissions earned on fees charged, but you want to give a break to your best clients. You can type in an IF statement in each cell in your workbook, or you can create a single function with an If-Then-Else statement.

Here is the code to create our user-defined Commission() function:

Function Commission(Fee)
    If Fee <= 1000 Then Commission = Fee * 0.1 Else Commission = Fee * 0.05
End Function

This function will charge a commission of 10 percent for fees less than or equal to $1,000 and 5 percent on all other fees.

While the commission calculation formula would be simple enough to include in each cell individually, it could quickly become a quagmire if we had a more complicated set of criteria to run through. In such instances, a user-defined function can save us lots of trouble.

Our user-defined function operates the same way as any other Excel function operates, as you can see in Figure B. The cells in Column C contain the Commission() function and calculate the commission based on the criteria provided by the user-defined function we created.

Figure B

Example calculation

With this technique, you can create very complicated functions and applications without resorting to extended formulas that reside within the cells themselves. This means your Excel applications can be easier to follow and adjust in the future.

  • Save
  • Print
  • Recommend
  • 10

Print/View all Posts Comments on this article

User-defined function library Mark W. KaelinTechrepublic Moderator | 08/17/04
User Defined Function rhp106@... | 08/25/04
The joy of mathematics Mark W. KaelinTechrepublic Moderator | 08/27/04
Function does not work iamthegun@... | 09/07/04
Function Does work if... andrew@... | 09/07/04
Functions you can use again and again martin.pendleton@... | 09/11/04
Nice Tip! techrep@... | 10/11/06
ask user to define, fills parameters misscrf | 09/14/04
FAQ misscrf | 09/15/04
Excellent!! Mark W. KaelinTechrepublic Moderator | 09/15/04
UDF greg@... | 11/29/06
What if you dislike VBA? JRynd | 08/20/04
What if you dislike....... Topic | 01/05/05
An easier method ike brasil | 10/11/06
Part of the Solution cindy_bond@... | 10/13/06
RE: Create a user-defined function in Microsoft Excel larry.williams@... | 03/05/08
RE: Create a user-defined function in Microsoft Excel morales_anarose@... | 07/01/08
RE: Create a user-defined function in Microsoft Excel moha_ali_250@... | 07/02/08

What do you think?

White Papers, Webcasts, and Downloads

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

SmartPlanet

Click Here