Help with creating macro's

CurtHoman

New Member
Joined
Mar 30, 2014
Messages
2
I'm new to this site, and I'm new to macros.Does anyone have an Excel macro that can give you the area of a triangle. Using this formula: area = Sqrt. (S x (s-a) x (s-b) x (s-c))
Where S = (a+b+c)/2
Sqrt. = Square Root of
x = multiplication
Any help is appreciated!
Thanks.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Give this macro a try...
Code:
Sub AreaOfTriangleBySides()

  Dim SideA As Double, SideB As Double, SideC As Double, S As Double, Area As Double
  
  SideA = Application.InputBox("What is side A?")
  If SideA = False Then Exit Sub
  If SideA <= 0 Then
    MsgBox "The side of a triangle must be a positive number greater than 0."
    Exit Sub
  End If
  
  SideB = Application.InputBox("What is side B?")
  If SideB = False Then Exit Sub
  If SideB <= 0 Then
    MsgBox "The side of a triangle must be a positive number greater than 0."
    Exit Sub
  End If
  
  SideC = Application.InputBox("What is side C?")
  If SideC = False Then Exit Sub
  If SideC <= 0 Then
    MsgBox "The side of a triangle must be a positive number greater than 0."
    Exit Sub
  End If
  
  S = (SideA + SideB + SideC) / 2
  Area = Sqr((S - SideA) * (S - SideB) * (S - SideC))
  MsgBox "The area of the triangle with sides " & SideA & _
         ", " & SideB & " and " & SideC & " is... " & Area

End Sub
 
Upvote 0
Just to remember

Conditions to build a triangle
Each side must be
1. less than the sum of the other two sides
2. greater than the absolute value of the difference of the other two sides

In other words:
Be a, b and c the sides of the triangle, then:
| b - c | < a < b + c
| a - c | < b < a + c
| a - b | < c < a + b

M.
 
Upvote 0
Just to remember

Conditions to build a triangle
Each side must be
1. less than the sum of the other two sides
2. greater than the absolute value of the difference of the other two sides

In other words:
Be a, b and c the sides of the triangle, then:
| b - c | < a < b + c
| a - c | < b < a + c
| a - b | < c < a + b
Good point! I can handle that easily enough in the code I posted with an On Error trap (if the sides are not specified correctly, the argument to the Sqr function will be negative and the function will error out)... the code I added to do this is highlighted in red.

Rich (BB code):
Sub AreaOfTriangleBySides()

  Dim SideA As Double, SideB As Double, SideC As Double, S As Double, Area As Double
  
  SideA = Application.InputBox("What is side A?")
  If SideA = False Then Exit Sub
  If SideA <= 0 Then
    MsgBox "The side of a triangle must be a positive number greater than 0."
    Exit Sub
  End If
  
  SideB = Application.InputBox("What is side B?")
  If SideB = False Then Exit Sub
  If SideB <= 0 Then
    MsgBox "The side of a triangle must be a positive number greater than 0."
    Exit Sub
  End If
  
  SideC = Application.InputBox("What is side C?")
  If SideC = False Then Exit Sub
  If SideC <= 0 Then
    MsgBox "The side of a triangle must be a positive number greater than 0."
    Exit Sub
  End If
  
  On Error GoTo BadSides
  S = (SideA + SideB + SideC) / 2
  Area = Sqr((S - SideA) * (S - SideB) * (S - SideC))
  MsgBox "The area of the triangle with sides " & SideA & _
         ", " & SideB & " and " & SideC & " is... " & Area
  Exit Sub

BadSides:
  MsgBox "Something is wrong with the lengths of the sides you specified."
  
End Sub
 
Upvote 0
Rick,

Suppose
SideA=2
SideB=8
SideC=10

In this case the formula results in Area = 0 (zero), what it's not correct
The SideC does not fulfill the condition
c < a+b

So I think the code should check the 3rd side in some way like this

If SideC >= (SideA + SideB) OR SideC <= ABS(SideA - SideB) then Goto ErrorTrap

M.
 
Upvote 0
Rick,

Suppose
SideA=2
SideB=8
SideC=10

In this case the formula results in Area = 0 (zero), what it's not correct
To me, that is not a triangle... it seems more like a straight line to me; but, with that said, my code will answer with an area of 0 for those numbers, no separate testing required.
 
Upvote 0
Exactly, that is not a triangle!

Well, actually it's not a big problem if the code returns 0. Just strange...

M.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top