How to create a User Defined Function to determine the type of triangle in Excel?


Introduction

Manually identifying the type of triangle among the massive datasets is challenging, time-consuming, and susceptible to errors. VBA is one of the prominent applications in Excel where users can customize the functions and resolve complex tasks efficiently. In this article, we will develop the user-defined function in the VBA to identify the triangle’s type.

Key Points to Remember Before Writing the User-defined Function

  • Equilateral triangles are those whose all three sides are equal.

  • Isosceles triangles are those whose only two sides are equal.

  • Scalene Triangles if all three of their sides are distinct.

Creation of a User-defined Function to Identify the Triangle’s Type According to the Given Input Data.

Step 1

Consider the sample dataset as shown in below image −

Step 2

Press ALT +F11 key to open the VBE window, switch to the “Insert” tab, and then choose the “Module” option as given below −

Step 3

Write the code in the new module as depicted below −

Function Type_of_triangle(m, n, p)
'Sorting the sides of a triangle'
If n > m Then r = m: m = n: n = r
If p > m Then r = m: m = p: p = r
If p > n Then r = p: p = p: p = r
'Identify the type of triangle'
If m > n + p Then
Type_of_triangle = "Default"
ElseIf m * m = n * n + p * p Then
Type_of_triangle = "Right"
ElseIf (m = n) And (n = p) Then
Type_of_triangle = "Equilateral"
ElseIf (m = n) Or (n = p) Then
Type_of_triangle = "Isosceles"
Else
Type_of_triangle = "Scalene"
End If
End Function

Save the program.

Step 4

Switch to the Excel worksheet and enter the formula =Type_of_triangle(a2,b2,c2) in the D2 cell and press the Enter key.

Step 5

Similarly, drag the formula down to the remaining cell range “D3:D5” to determine the type of the triangle according to the given sides. The resultant values are highlighted in the below image −

Conclusion

You experience an enormous dataset, attempt these strategies, and experience the convenience of swiftly determining the various types of triangles in your Excel column. If else statements are declared inside the UDF to check certain conditions. The users must invoke the defined function in the specified cell to get the result.

Updated on: 22-Dec-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements