How do I create a user defined function in Excel VBA?

There are some things you cannot do easily in Excel without being able to create a user defined function.

User defined functions allow you to enhance the capabilities of Excel.

But “How do I create a user defined function in Excel VBA?”

Consider this example:

custom_function_vba_1

You could probably work out a way to get this result, but why not just create a udf, (user define function)?

Here’s an example:

Const dblTax As Double = 0.0825


Function GetGTotal(Price) As Double
    Dim dblGtotal As Double
    
    
    dblGtotal = (Price * dblTax) + Price
    
    GetGTotal = dblGtotal
    
End Function

In the destination cell (B2), you’ll just type in “=GetGTotal(B1)”

Also:

In this example, we are getting a total of the sales values and then calculating the commission for the total sales amount:

Function GetCommission(total) As Double
    Dim dblCommission As Double
    
    dblCommission = total * 0.1
    
    GetCommission = dblCommission
    
    
End Function

In the destination cell (A10), you’ll just type in “=getcommission(A9)”

Click here and let me know if you have any questions.


Posted

in

by

Tags: