How to create and test the User Defined Function to compute the Area of a Triangle in Excel 365?


Introduction

VBA is a crucial component of Microsoft Excel that facilitates users to develop the function and subroutine that can be executed in the Excel worksheet. The value would be retrieved by the user-defined function and given to the specific cells that call it. Users must employ the indentation in the VBA code to enhance the readability of the code. In this article, we will explore the efficient techniques for calculating the area of a triangle and comparing values. Cell formatting is not done by the user-defined function.

To Generate the User Defined Function to Compute the Area of the Triangle

Step 1

Write the following entries in the range A1:E5 along with the column heading as depicted below image −

Step 2

Enter the formula =0.5*A2*B2*SIN(RADIANS(C2) in the D2 cell to evaluate the area of a triangle and then press the “Enter” key.

Step 3

Similarly, drag the formula down to fill the remaining cells range D3: D5 as shown below −

Step 4

Pree ALT +F11 key to open VBE window, click on the “Insert” tab and then select the “Module” option as given below −

Step 5

Write the code in the new module as depicted below −

'creation of user defined function'
Function Ar_Traingle(t1, t2, Theta)
'compute formula'
Ar_Traingle = 0.5 * t1 * t2 * Sin(WorksheetFunction.Radians(Theta))
End Function

Step 6

Save the worksheet and then switch to the Excel worksheet and type the UDF =Ar_Triangle(A2,B2,C2) in the E2 cell. Users need to give the proper cell reference inside the function otherwise Error may arise. Then press the “Enter” key to obtain the computed area.

Step 7

Similarly, drag the + sign at the right bottom corner of the E2 cell and drop it to the E5 cell to evaluate the function of the pending cells as shown below −

If we compare the value of the formula which is computed in the D column with the user- defined function in the E column then the result is the same in both columns.

Conclusion

The users learned the tricky tip to evaluate the area of the triangle if only two side values and an angle of the triangle are given. Users must write the proper syntax of the function, variable declaration, and area formula correctly in the module to acquire the correct result. The indentation part in the VBA function is optional.

Updated on: 22-Dec-2023

29 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements