# Trending December 2023 # How To Use Val Function In Excel Vba With Excel Template # Suggested January 2024 # Top 21 Popular

You are reading the article How To Use Val Function In Excel Vba With Excel Template updated in December 2023 on the website Daihoichemgio.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 How To Use Val Function In Excel Vba With Excel Template

VBA Val Function

VBA Val stands for Value. It converts the arrays or string which has some numbers into pure numerical values. Suppose if we give “111 One” as input then we will get only “111” as numerical output. This is quite useful while working in a data which we extract from some kind of database. In that database file, we may encounter such cells which may contain numbers along with extra spaces, hidden characters, special characters or alphabets. In that case, using Val can convert that string into numbers. Which can be used in further analysis.

How to Use Excel Val Function in VBA?

Let’s see the examples of Val in Excel VBA.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

You can download this VBA VAL Excel Template here – VBA VAL Excel Template

Example #1 – VBA Val

It is quite easy to implement. For multiple types of applications and codes, we will first form a frame of code which we will be using multiple times in further examples.

Step 1: Go to Insert menu tab and select a Module as shown below.

Step 2: After that, we will get the blank window of Module. In that, write the sub category of VBA Val in excel or you can use any other name of subcategory as per your need.

Code:

Sub

VBA_Val()

End Sub

Code:

Sub

VBA_Val()

Dim

A

As Variant

End Sub

Step 4: And now assign any type of number sequence to variable A under VBA function VAL. We have assigned a combination of sequential number for demonstration as shown below with spaces between them.

Code:

Sub

VBA_Val() Dim A As Variant A = Val("11 22 33")

End Sub

Step 5: At last we will need a message box to print the values stored in variable A.

Code:

Sub

VBA_Val()

Dim

A

As Variant

A = Val("11 22 33") MsgBox A

End Sub

Example #2 – VBA Val

In this example, we will see how VBA Val function is used for number containing some mathematical signs. For this, we will consider the code written above. We have taken out the frame of the code which will be used all the examples, as shown below.

Step 1: Go to Insert menu tab and select a Module as shown below

Step 2: As highlighted in the below screenshot, we will keep updating the value between brackets of VAL Function.

Code:

Sub

VBA_Val2()

Dim

A

As Variant

A = Val("") MsgBox A

End Sub

Step 3: Now let’s insert any number with mathematical sign plus (“+”) as shown below.

Code:

Sub

VBA_Val2()

Dim

A

As Variant

A = Val("+111") MsgBox A

End Sub

Step 4: Now compile and run the code. We will see, VBA Val has given the values as 111 without the plus sign. It is because logically all the values with or without plus signs are always positive in nature.

Step 5: Let’s change the value in Val function from +111 to -111. Now we will see if minus sign gets converted into the value or not.

Code:

Sub

VBA_Val2()

Dim

A

As Variant

A = Val("-111") MsgBox A

End Sub

Step 6: Compile the code and run. We will see, the minus sign is still retained in the value and message box has returned the value as -111. Which means any sign other than plus will not get converted with Val function in VBA.

Example #3 – VBA Val

In this example, we will see, how Val function would work for time formats.

Step 1: For this again we will use the above-defined format for Excel VBA Val as shown below.

Code:

Sub

VBA_Val3()

Dim

A

As Variant

A = Val("") MsgBox A

End Sub

Step 2: Now insert any time format in VAL function as circled in the above screenshot. Here we are adding 11 AM as shown below.

Code:

Sub

VBA_Val3()

Dim

A

As Variant

A = Val("11 AM") MsgBox A

End Sub

Step 3: Now compile the code and run. We will see, VAL function has eliminated AM from 11 AM and given us only 11 as output as shown below.

Step 4: Now let’s use some different format. Use any minutes with hours. We have used value 11:05 under Val brackets.

Code:

Sub

VBA_Val3()

Dim

A

As Variant

A = Val("11:05 AM") MsgBox A

End Sub

Step 5: Again compile and run the code. Again Val function has removed colon and minutes numbers along with AM and given us the whole number 11 as shown below.

Example #4 – VBA Val

In this example, we will see how the date format works in this.

Code:

Sub

VBA_Val4()

Dim

A

As Variant

A = Val("") MsgBox A

End Sub

Step 2: Now insert any date format as per your need. We can insert data in a hyphen (“-“) format in a slash (“ / “) format. Let’s use the slash date format which is most often used.

Code:

Sub

VBA_Val4()

Dim

A

As Variant

A = Val("06/26/2023") MsgBox A

End Sub

Step 3: Now compile the code and run it. We will see VBA Val has returned the numerical values as “6”. Values after slash are not accepted by VBA Val.

Example #5 – VBA Val

In this example, we will see how this will work when the numbers are after the text.

Step 1: Take the format which we have seen above.

Code:

Sub

VBA_Val2()

Dim

A

As Variant

A = Val("") MsgBox A

End Sub

Step 2: In Val function brackets, let’s put some text and numbers. Let’s consider “AB 11” as shown below.

Code:

Sub

VBA_Val2()

Dim

A

As Variant

A = Val("AB 11") MsgBox A

End Sub

Step 3: Now run it. We will see, in the message box, only 0 is appearing. Which means VBA Val doesn’t consider the numbers after characters or text.

Pros of VBA Val

It can be used in the data which is extracted from some kind of tool or database. Which consists of different kind of characters along with numbers.

It is quite easy to separate numbers by using VBA Val in any kind data.

We can choose any format which consists of a number or set of number to separate it from other characters.

Things to Remember

It also considers the decimals.

Save the file as Marco enable excel so that written would be retained.

If record this process in VBA, then obtained code will be much lengthier than the examples which we have seen above.

There is not an alternate insert function available in Excel which gives the same result as VBA Val.

Recommended Articles

This is a guide to VBA Val. Here we discuss how to get Val in VBA Excel along with practical examples and downloadable excel template. You can also go through our other suggested articles –

You're reading How To Use Val Function In Excel Vba With Excel Template

## How To Use Excel Vba Match Function?

Excel VBA Match Function

VBA Match Function looks for the position or row number of the lookup value in the table array i.e. in the main excel table. For example, VLOOKUP, HLOOKUP, MATCH, INDEX, etc. These are the lookup functions that are more important than others. Regretfully, we don’t have the same functions available in VBA for making things easier. However, we can use these functions as worksheet functions under the VBA script to make our lives easier.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Today, we are about to learn the MATCH function which can be used as a worksheet function under VBA.

VBA Match has the same use as the Match formula in Excel. This function in MS Excel VBA finds a match within an array with reference to the lookup value and prints its position. This function becomes useful when you need to evaluate the data based on certain values. For example, VBA MATCH is helpful if you have the salary data of employees and you need to find out the numeric position of an employee in your data who has salary less than/greater than/equals to a certain value. It is really helpful in analyzing the data and also one line of code can automate the things for you.

Syntax of Match Function in Excel VBA

VBA Match has the following syntax:

Arg1 – Lookup_value – The value you need to lookup in a given array.

Arg2 – Lookup_array – an array of rows and columns which contain possible Lookup_value.

Arg3 – Match_type – The match type which takes value -1, 0 or 1.

If match_type = -1 means that the MATCH function will find out the smallest value which is greater than or equals to the lookup_value. For this to happen, the lookup_array must be sorted in descending order.

If match_type = 0 means that the MATCH function will find out the value which is exactly the same as that of the lookup_value.

If match_type = +1 it means that the MATCH function will find out the largest value which is lesser than or equals to the lookup_value. For this to happen, the lookup_array must be sorted in ascending order. The default value for the match type is +1.

How to Use Excel VBA Match Function?

We will learn how to use a VBA Match Excel function with few examples.

You can download this VBA Match Excel Template here – VBA Match Excel Template

VBA Match Function – Example #1

Suppose we have data as shown below:

We need to find who from this list have salary € 30,000 along with position in Excel.

Though in this data set we can manually configure this, please think on a broader picture, what if you have millions of rows and columns?

Follow the below steps to use MATCH function in VBA.

Step 1: Define a sub-procedure by giving a name to macro.

Code:

Sub

exmatch1()

End Sub

Step 2: Now, we want our output to be stored in cell E2. Therefore, start writing the code as Range(“E2”).Value =

This defines the output range for our result.

Code:

Sub

exmatch1() Range("E2").Value =

End Sub

Step 3: Use WorksheetFunction to be able to use VBA functions.

Code:

Sub

exmatch1() Range("E2").Value = WorksheetFunction

End Sub

Step 4: WorksheetFunction has a variety of functions that can be accessed and used under VBA. After “WorksheetFunction”, put a dot (.) and then you’ll be able to access the functions. Choose the MATCH function from the dropdown list.

Code:

Sub

exmatch1() Range("E2").Value = WorksheetFunction.Match

End Sub

Code:

Sub

exmatch1() Range("E2").Value = WorksheetFunction.Match(Range("D2").Value,

End Sub

Step 6: The Second argument is Lookup_array. This is the table range within which you want to find out the position of Lookup_value. In our case, it is (B1:B11). Provide this array using the Range function.

Code:

Sub

exmatch1() Range("E2").Value = WorksheetFunction.Match(Range("D2").Value, Range("B1:B11"),

End Sub

Code:

Sub

exmatch1() Range("E2").Value = WorksheetFunction.Match(Range("D2").Value, Range("B1:B11"), 0)

End Sub

Step 8: Run this code by hitting F5 or Run button and see the output.

You can see in Cell E2, there is a numeric value (6) which shows the position of the value from cell D2 through range B1:B11.

Example #2 – VBA Match Function with Loops

It is easy when you have only one value to lookup for in the entire range. But, what if you need to check the position for a number of cells? It would be harsh on a person who is adding to ask him to write the separate codes for each cell.

In such cases, MATCH function can be used with loop (especially For loop in our case). See the following steps to get an idea of how we use MATCH function with loop.

Code:

Sub

Example2()

End Sub

Step 2: Define an integer that can hold the value for multiple cells in the loop.

Code:

Sub

Example2()

Dim

i

As Integer

End Sub

Step 3: Use For loop on the integer to use the different lookup values whose position can be stored in column E.

Code:

Sub

Example2()

Dim

i

As Integer

For

i = 2

To

6

End Sub

Step 4: Now, use the same method we used in example 1, just instead of Range, we will use the Cells function and will be using a two-dimensional array (Rows and columns) in contrast to the first example.

Code:

Sub

Example2()

Dim

i

As Integer

For

i = 2

To

6 Cells(i, 5).Value = WorksheetFunction.Match(Cells(i, 4).Value, Range("B2:B11"), 0)

Next

i

End Sub

Here, Cells(i, 5).Value = stores the value of resulting positions in each row from 2 to 6 (row i) in column E (column number 5). Under Match function, Cells(i, 4).Values checks for each Lookup_value present in row 2 to 6 in the 4th column. This lookup value then searched in Array B2:B11 in excel sheet where data is present and relative positions can be stored in each row of column 5 (column E).

Step 5: Run this code by hitting F5 or Run button simultaneously and see the result. It will almost pull out the magic in a piece of code with a single line.

In this article, we learned how we can use MATCH function under VBA as a special case of WorksheetFunction.

Things to Remember

Lookup_value can be number/text/logical value or can be a cell reference to a number, text or logical value.

By default, Match_type can be considered as 1, if omitted/not mentioned.

As similar to Excel MATCH function, VBA MATCH also gives the relative position of a Lookup_value under Lookup_array and not the value itself.

If a match is not found, a relative excel cell will be filled with #N/A.

If MATCH function is used on Text values, it is not able to differentiate between lowercases and uppercases. For Example, Lalit and lalit are same. So do LALIT and lalit.

Wildcard characters can be used if you are finding out the exact match (i.e. match type is zero). Wildcard character asterisk (*) can be used to find out a series of characters. While a question mark (?) can be used to find out a single character.

Recommended Articles

This is a guide to VBA Match Function. Here we discuss VBA Match and how to use Excel VBA Match Function along with practical examples and downloadable excel template. You can also go through our other suggested articles –

## Ipmt In Excel How To Use Ipmt Function In Excel? (With Examples)

IPMT Function in Excel

IPMT Function calculates a specific portion of interest based on the loan amount and tenure. The syntax of IPMT is quite similar to the syntax of PV Function in Excel, which all have seen earlier. To understand better, IPMT helps used to distinguish between different portions or segments of any loan and to what time how small amount is to be paid based on the interest applicable can be calculated.

IPMT Formula in Excel:

Excel functions, formula, charts, formatting creating excel dashboard & others

Explanation of IPMT Function in Excel

There are six parameters used for the IPMT function. Four parameters are compulsory, and two are optional.

Parameter details are as follows:

Compulsory Parameters:

Rate: The interest rate per period.

Per: The period for which you want to find the interest and must be in the range 1 to n per.

Nper: The total number of payment periods in an annuity.

Pv: The present value, or the lump-sum amount that a series of future payments is worth right now.

Optional Parameters:

[FV]: It is an optional argument. The FV or a cash balance you want to attain after the last payment is made. If FV is omitted, excel assumes it to be 0 (the future value of a loan, for example, is 0).

[Type]: This is also an optional argument. The number 0 or 1 indicates when payments are due. If this argument is omitted, Excel assumes it to be 0.

The Type can be 0 or 1, where:

1 = The payment is made at the start of the period.

How to Use the IPMT Function in Excel?

IPMT function in Excel can be used as a worksheet function and a VBA Function. Here are some examples of the IPMT functions to understand the working of the IPMT function in Excel.

You can download this IPMT Function in Excel Template here – IPMT Function in Excel Template

Example #1

Interest payment made for months 1 and 2 of a loan of \$70,000, which is to be paid after 6 years. An interest rate of 6% per year, and the payment to the loan is to be made at the end of each month.

Result is :

To convert the annual interest rate of 6% into the monthly rate (=6%/12) and the number of periods from years to months (=6*12).

Example #2

Interest during quarters 1 and 2 of an investment is required to increase investment from \$0 to \$6,000 over 3 years. The interest rate of 4.5% per year, and the payment into the investment is to be made at the beginning of each quarter.

Result is :

The annual interest rate has been converted into a quarterly rate (4.5%/4)

The number of periods has been converted from years to quarters (=3*4).

The [type]argument has been set to 1 to indicate that the payment will be made at the start of each quarter.

The interest for the first quarter is zero, as the first payment is made at the start of the quarter.

Note:

* Use N%/12 for rate and N*12 for nper when there are monthly payments. N%/4 for rate and N*4 for nper when quarterly payments and N% for rate and N for nper when there are annual payments.

*Cash paid out is shown as negative numbers. Cash received is shown as positive numbers.

Things to Remember

Below are a few error details that can come in the IPMT function as the wrong argument will be passed in the functions.

2. Error handling #VALUE!: IPMT function through a #VALUE! Error when any non-numeric.

VBA Function Example:

The IPMT function can also be used in VBA code.

For example:

Recommended Articles

This has been a guide to IPMT in Excel. Here we discuss the IPMT Formula in Excel and how to use the IPMT function in Excel, along with practical examples and downloadable Excel templates. You can also go through our other suggested articles –

## Calculator (Example With Excel Template)

Start Your Free Investment Banking Course

What is the Cost-Benefit Analysis Formula?

The term “cost-benefit analysis” refers to the analytical technique that compares the benefits of a project with its associated costs. In other words, all the expected benefits out a project are placed on one side of the balance and the costs that have to be incurred are placed on the other side. The cost-benefit analysis can be executed either using “benefit-cost ratio” or “net present value”.

The formula for a benefit-cost ratio can be derived by dividing the aggregate of the present value of all the expected benefits by an aggregate of the present value of all the associated costs, which is represented as,

Benefit-Cost Ratio = ∑PV of all the Expected Benefits / ∑PV of all the Associated Costs

The formula for net present value can be derived by deducting the sum of the present value of all the associated costs from the sum of the present value of all the expected benefits, which is represented as,

Net Present Value = ∑PV of all the Expected Benefits – ∑PV of all the Associated Costs

Example of Cost-Benefit Analysis Formula (With Excel Template)

Let’s take an example to understand the calculation of Cost-Benefit Analysis in a better manner.

You can download this Cost-Benefit Analysis Excel Template here – Cost-Benefit Analysis Excel Template

Cost-Benefit Analysis Formula – Example #1

Let us take the example of a financial technology start-up which is contemplating on hiring two new programmers. The promoter expects the programmers to increase the revenue by 25% while incurring an additional cost of \$45,000 in the next one year. The help promoter decides whether to go ahead with the recruitment based on cost-benefit analysis if the revenue of the company in the current year is \$220,000 and the relevant discount rate is 5%.

Solution:

PV of Benefit is Calculated as:

PV of Benefit= \$55,000 / (1 + 5%)

PV of Benefit = \$52,380.95

PV of Cost is Calculated as:

PV of Cost = \$35,000 / (1 + 5%)

PV of Cost = \$33,333.33

Benefit-Cost Ratio is calculated using the formula given below

Benefit-Cost Ratio = ∑PV of all the Expected Benefits / ∑PV of all the Associated Costs

Benefit-Cost Ratio = \$52,380.95 / \$33,333.33

Benefit-Cost Ratio = 1.57x

Net Present Value is calculated using the formula given below

Net Present Value = ∑PV of all the Expected Benefits – ∑PV of all the Associated Costs

Net Present Value = \$52,380.95 – \$33,333.33

Net Present Value = \$19,047.62

Therefore, both the method of cost-benefit analysis suggests that the promoter should go ahead with the recruitment.

Cost-Benefit Analysis Formula – Example #2

Let us take the example of two projects to illustrate the use of cost-benefit analysis. The sum of the present value of expected benefits from Project 1 is \$50 million with the sum of the present value of associated costs of \$30 million. On the other hand, the sum of the present value of expected benefits from Project 2 is \$10 million with the sum of the present value of associated costs of \$5 million. Discuss which project is better based on cost-benefit analysis.

Solution:

Benefit-Cost Ratio is calculated using the formula given below

Benefit-Cost Ratio = ∑PV of all the Expected Benefits / ∑PV of all the Associated Costs

For Project 1

Benefit-Cost Ratio = \$50,000,000 / \$30,000,000

Benefit-Cost Ratio = 1.67x

For Project 2

Benefit-Cost Ratio = \$10,000,000 / \$5,000,000

Benefit-Cost Ratio = 2.00x

Net Present Value is calculated using the formula given below

Net Present Value = ∑PV of all the Expected Benefits – ∑PV of all the Associated Costs

For Project 1

Net Present Value = \$50,000,000 – \$30,000,000

Net Present Value = \$20,000,000

For Project 2

Net Present Value = \$10,000,000 – \$5,000,000

Net Present Value = \$5,000,000

Explanation

The formula for cost-benefit analysis can be calculated by using the following steps:

Step 1: Firstly, Calculate all the cash inflow from the subject project, which is either revenue generation or savings due to operational efficiency.

Step 2: Next, Calculate all the cash outflow into the project, which are the costs incurred in order to maintain and keep the project up and running.

Step 3: Next, Calculate the discounting factor based on the current pricing of assets with a similar risk profile.

Step 4: Next, based on the discounting factor, calculate the present value of all the cash inflow and outflow. Then, add up the present value of all the cash inflow as ∑PV of all the expected benefits and outflow as ∑PV of all the associated costs.

Step 5: Now, the formula for a benefit-cost ratio can be derived by dividing aggregate of the present value of all the expected benefits (step 4) by aggregate of the present value of all the associated costs (step 4) as shown below.

Benefit-Cost Ratio = ∑PV of all the Expected Benefits / ∑PV of all the Associated Costs

Step 6: Now, the formula for net present value can be derived by deducting the sum of the present value of all the associated costs (step 4) from the sum of the present value of all the expected benefits (step 4) as shown below.

Net Present Value = ∑PV of all the Expected Benefits – ∑PV of all the Associated Costs

Relevance and Use of Cost-Benefit Analysis Formula

The importance of cost-benefit analysis lies in the fact that it is used for assessing the feasibility of an opportunity, comparing projects, appraising opportunity cost and building real-life scenario-based sensitivity testing. In this way, this technique helps in ascertaining the accuracy of an investment decision and provides a platform for its comparison with similar proposals.

Cost-Benefit Analysis Formula Calculator

You can use the following Cost-Benefit Analysis Formula Calculator

∑PV of all the Expected Benefits ∑PV of all the Associated Costs Benefit-Cost Ratio   Benefit-Cost Ratio = ∑PV of all the Expected Benefits =

∑PV of all the Associated Costs

0

= 0

0

Recommended Articles

This is a guide to Cost-Benefit Analysis Formula. Here we discuss how to calculate the Cost-Benefit Analysis Formula along with practical examples. We also provide a Cost-Benefit Analysis calculator with a downloadable excel template. You may also look at the following articles to learn more –

## Calculator (Examples With Excel Template)

Start Your Free Investment Banking Course

What is Equity Formula?

Equity = Total Assets – Total Liabilities

There is another method to derive the equity of a company. In this method, all the different classes of equity capital, which includes common/capital stock, share premium, preferred stock, retained earnings and accumulated other comprehensive income, are added while the treasury stocks are deducted. Mathematically, it is represented as,

Equity = Capital Stock + Share Premium + Preferred Stock + Retained Earnings + Accumulated Other Comprehensive Income – Treasury Stock

Examples of Equity Formula (With Excel Template)

Let’s take an example to understand the calculation of Equity in a better manner.

You can download this Equity Formula Excel Template here – Equity Formula Excel Template

Equity Formula – Example #1

Let us take the example of a company ABC Ltd that has recently published its annual report for the financial year ending on December 31, 2023. As per the balance sheet, the total assets of the company stood at \$500,000, while its total liabilities stood at \$300,000 as on December 31, 2023. Determine ABC Ltd’s equity as on the balance sheet date.

Solution:

Equity is calculated using the Formula given below.

Equity = Total Assets – Total Liabilities

Equity = \$500,000 – \$300,000

Equity = \$200,000

Therefore, ABC Ltd’s equity stood at \$200,000 as on December 31, 2023.

Equity Formula – Example #2

Let us take the Real-Life example of Airbus SE’s published annual report as on December 31, 2023. As per the balance, the information is available. Calculate Airbus SE’s equity based on the given information.

Equity is calculated using the Formula given below.

Equity = Capital Stock + Share Premium + Retained Earnings + Accumulated Other Comprehensive Income – Treasury Stock

Equity = €777 + €2,941 + €5,923 + €134 – €51

Equity = €9,724 million

Therefore, Airbus SE’s equity stood at €9,724 million as of December 31, 2023.

Explanation

The formula for equity can be derived by using the following steps:

Step 1: Firstly, determine the total assets of the company, which is the last line item on the asset side of the balance sheet and includes plant, machinery, cash, bank deposits, investments, etc.

Step 2: Next, determine the total liabilities of the company, which is also available in the balance sheet and includes all kinds of debt obligations, payables, etc.

Step 3: Finally, the formula for equity can be derived by subtracting the total liabilities (step 2) from the total assets (step 1) as shown below.

Under the other method, the formula for equity can be derived by using the following steps:

Step 1: Firstly, identify all the different categories of equity capital from the balance sheet.

Step 2: Finally, the formula for equity can be derived by adding up all the categories of equity capital except ones that have been repurchased and retired (also known as treasury stock) as shown below.

Equity = Capital stock + Share premium + Preferred stock + Retained earnings + Accumulated other comprehensive income – Treasury stock

Relevance and Uses of Equity Formula

From the perspective of an investor or an investment analyst, it is important to understand the concept of equity because it predominantly used to evaluate the real value of a company (net worth). In fact, the value of one’s equity investment in the company is captured by the equity value and as such the shareholders are typically concerned with the net worth of the company.

The value of equity can be both positive or negative. A positive equity value indicates that the company has adequate total assets to pay off its total liabilities. On the other hand, a negative value of equity indicates that the company may be on the way to become insolvent as the total liabilities exceed its total assets. Consequently, the investor community, in general, considers a company to be risky and perilous if it has a negative equity value. However, the value of equity in isolation may not give very meaningful insight into a company’s financial health. But an investor can use the equity value to analyze the company to draw significant conclusions if it is used in combination with other financial metrics.

Equity Formula Calculator

You can use the following Equity Formula Calculator.

Total Assets Total Liabilities Equity   Equity = Total Assets

Total Liabilities =

0

0

= 0

Recommended Articles

This is a guide to Equity Formula. Here we discuss how to calculate Equity along with practical examples. We also provide an Equity calculator with a downloadable excel template. You may also look at the following articles to learn more –

## User Defined Function In Excel

Excel functions, formula, charts, formatting creating excel dashboard & others

Introduction to Excel User Defined Function

Excel has reached a source of inbuilt functions to make your life easy when analyzing data. However, that is not the end. Sometimes, you might come up with a situation where all the formulae provided must be more suitable for your task. In such cases, you can create your function in Excel with the help of VBA; such functions are called User Defined Functions (UDFs). UDFs can be used within the worksheet the same way you use the normal Excel functions such as SUM, AVERAGE, etc. These functions can be created per the user’s requirement through VBA and ease the user’s tasks in Excel. In this article, we will discuss the User Defined Function in Excel.

Any UDF has a basic rule; you need to define it under a FUNCTION procedure instead of a subroutine procedure (sub-procedure we generally use). Using the FUNCTION procedure, we can create UDFs and use them inside Excel as the general Excel functions such as SUM, AVERAGE, VLOOKUP, etc.

Examples to Create User Defined Function

Let’s understand how to create the User Defined Functions in Excel with some examples.

You can download this User Defined Function Excel Template here – User Defined Function Excel Template

Example #1 – User Defined Function to Find the Area of Triangle

Students from the mathematical stream may always have a problem where they need to find a triangle area. Though it has a very easy formula to work around, students will be pleased if they have some function that can compute the area of a triangle. Follow the steps below to create a function that can compute the area of a triangle.

Step 2: Define a new FUNCTION procedure with the name as TriangleArea. This name will be used to find this function in Excel.

Since it is a FUNCTION procedure, we must set the arguments to calculate the triangle area while defining the function.

Step 3: After TriangleArea, mention height and base as two arguments to this function in the bracket. These are the mandatory arguments. The user needs to provide values for height and base to calculate a triangle’s area. The FUNCTION procedure is complete (you can see the End Function clause), and we can write pieces/lines of code within this procedure.

Step 4: Now, we will write a code that will return the area for a triangle when we provide the height and base values within this procedure. Since the area of the triangle is formulated as we will formulate it under the code and assign it to TriangleArea.

Suppose we have different height and base values, and we need to capture the area of a triangle for those values using the function we just have created. This is it; the function is created and can be used. Save the code, and we will see how it works for the different values of height and base.

Step 5: In cell C2, start typing the formula =TriangleArea(A2, B2) to compute the area of a triangle and provide height and base as an argument to the same formula.

Drag the formula to get the area for the second and third values of height and base.

Example #2 – User Defined Function to Square any Number

Suppose we want the squared value of any number (integer or float). No such function gives you the squared value for any number. Let’s create one that can do the task for you in Excel.

Step 1: In the same module, start defining a new FUNCTION procedure for squaring a number with the name SquareNum.

Step 2: We need to add an argument for the function. Use num as an argument to the function within parentheses. As soon as you provide an argument within the function, you will be able to see the End Function statement, which means the arguments we have provided are accepted,

Step 3: We can add the code/lines to get the square of numbers within the function. As num is the argument to the SquareNum function, we have two options to get the desired output. One way is to multiply num by itself (num * num ex. 2 * 2), or another way is to use the power operator “^” over the num function (num^2 ex. 3^2). I will go with the other way as it makes the code more versatile.

Suppose we have a set of different numbers, as shown in the image below, and we need to find the square values for those. We can use the SquareNum function defined just above to get the result.

Step 6: Provide A2 as an argument to this function so that it will square the number present in cell A2.

Step 7: Close the parentheses to complete the formula and press Enter key to see the square of number 11 (number in cell A2).

Step 8: Drag and Paste the formula across the remaining cells to find the square roots of the numbers provided.

This article emphasized creating User Defined Functions in Excel, which can reduce the man’s work. Let’s wrap things up with some points to be remembered.

Things to Remember About User Defined Functions in Excel

UDF, through VBA, helps create custom formulas that can reduce the time we need to do any task and simplify the tasks.

Recommended Articles

This is a guide to User Defined Functions in Excel. Here we discuss how to create a User Defined Function in Excel, practical examples, and a downloadable Excel template. You can also go through our other suggested articles –

Update the detailed information about How To Use Val Function In Excel Vba With Excel Template on the Daihoichemgio.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!