Your cart is currently empty!
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
- Press Alt + F11 in Excel.
- In the VBA Editor, go to Insert > Module.
- 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:
A | B | C | Type |
---|---|---|---|
5 | 5 | 5 | Equilateral |
6 | 6 | 8 | Isosceles |
7 | 5 | 4 | Scalene |
1 | 2 | 3 | Not 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
-
Excel Course in Hindi: Basic to Advanced Level
Original price was: ₹2,299.00.₹2,249.00Current price is: ₹2,249.00. -
Excel Training Program: Fundamentals to Advanced Techniques : Classroom/ Live Classes
Original price was: ₹6,000.00.₹4,000.00Current price is: ₹4,000.00. -
Excel VBA/Macro Masterclass: Automate Excel, Boost Productivity : Classroom /Live Class Training
Original price was: ₹8,500.00.₹6,500.00Current price is: ₹6,500.00. -
Gmail Mastery Training in Hindi: Unlocking Advanced Email Management Technique
Original price was: ₹899.00.₹849.00Current price is: ₹849.00. -
Gmail Mastery: Advanced Training for Efficient Email Management
Original price was: ₹899.00.₹849.00Current price is: ₹849.00.