How to Create a User Defined Function in Excel to Identify Triangle Types


🧠 Storytime: Why Rohan and Meera Needed a Triangle Function in Excel

Rohan and Meera are engineering students in Pune. While working on a school project about geometry and architecture, they had to classify different types of triangles based on side lengths.

They had a long list of side measurements in Excel. Typing the triangle types manually was slow and error-prone.

Meera asked, “Can’t we just create a formula in Excel that tells us if the triangle is Equilateral, Isosceles, or Scalene?”

Rohan replied, “Excel has IF and nested conditions, but it’ll get messy. Let’s write a User Defined Function using VBA!”

Thus began their journey into VBA.


🔧 What is a User Defined Function (UDF) in Excel?

A User Defined Function is a custom function written in VBA (Visual Basic for Applications) that works like a built-in Excel formula.

With a UDF, you can extend Excel’s capabilities beyond standard formulas.


🧮 Goal: Create a Function to Determine Triangle Type

Based on the lengths of the three sides, the function should return:

  • Equilateral – All three sides are equal.
  • Isosceles – Any two sides are equal.
  • Scalene – All sides are different.
  • Not a Triangle – The side lengths don’t form a valid triangle.

🛠 Step-by-Step: Creating the UDF in Excel

✅ Step 1: Open the VBA Editor

  1. Press Alt + F11 in Excel.
  2. In the VBA Editor, go to Insert > Module.
  3. A new module window opens.

✅ Step 2: Paste the VBA Code

Function TriangleType(a As Double, b As Double, c As Double) As String
    ' Check if the sides can form a triangle
    If a + b <= c Or a + c <= b Or b + c <= a Then
        TriangleType = "Not a Triangle"
    ElseIf a = b And b = c Then
        TriangleType = "Equilateral"
    ElseIf a = b Or b = c Or a = c Then
        TriangleType = "Isosceles"
    Else
        TriangleType = "Scalene"
    End If
End Function

✅ Step 3: Save and Return to Excel

  • Press Ctrl + S and close the VBA Editor.
  • Make sure your file is saved as .xlsm (Macro-enabled workbook).

📊 Step 4: Use the Function in Excel

In your worksheet, enter side lengths in three cells (say A2, B2, and C2), and in D2 write:

=TriangleType(A2, B2, C2)

✅ It will return one of:

  • “Equilateral”
  • “Isosceles”
  • “Scalene”
  • “Not a Triangle”

💡 Example:

ABCType
555Equilateral
668Isosceles
754Scalene
123Not a Triangle

📘 Bonus: Learn More with a Complete Excel Course!

Just like Rohan and Meera used Excel creatively, you can too!

📌 If you want to learn Excel from basic to advanced, including formulas, charts, data tools, and VBA, check out:

🎓 Mastering MS Excel – A Comprehensive Training Course

✔️ Learn practical Excel skills
✔️ Master formulas, charts, PivotTables, VBA & more
✔️ Ideal for students, professionals, entrepreneurs

👉 Enroll Now and level up your career with Excel mastery.


On sale products