PDA

View Full Version : auto draw triangle from cell values in worksheet



stramvaier
02-14-2019, 08:47 AM
Hi. i am trying to automatically draw a 90degree triangle in my excel sheet based on the unit value in 3 cells. i dont know if its possible so i ask here for help.

i have calculated the unit value for leg_a using cosinus of the angle between leg_a and hyp_c (converted from radians to degrees) and hypotenuse unit lenght(A5).
I will allways know these values (angle and hyp)
I then use phytagoras for leg_b, standard math.

This gives me the correct unit distance for each side, but i cant find how to make a triangle in my worksheet based on the value i have in these cells:
A5=hyp_c, E5=leg_a, F5=leg_b.
23746

I would also like the triangle to be dynamic so as when i change the values in my cells the triangle would change also.
anyone have a good idea to help me?

Any help is appreciated.

大灰狼1976
02-14-2019, 07:39 PM
Hi stramvaier!
The length of two right angles is enough.
something like below

Private Sub Worksheet_Change(ByVal Target As Range)
Dim obj As Object
For Each obj In Shapes
If obj.Name Like "Right Triangle *" Then
obj.Height = [f5]
obj.Width = [e5]
Exit Sub
End If
Next
Set obj = ActiveSheet.Shapes.AddShape(msoShapeRightTriangle, 200, 200, [f5], [e5])
End Sub

大灰狼1976
02-14-2019, 07:42 PM
Sorry! Forgot to add restrictive conditions.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address <> [e5].Address And Target.Address <> [f5].Address Then Exit Sub
If [e5] <= 0 Or [f5] <= 0 Then MsgBox "Input Error!": Exit Sub
Dim obj As Object
For Each obj In Shapes
If obj.Name Like "Right Triangle *" Then
obj.Height = [f5]
obj.Width = [e5]
Exit Sub
End If
Next
Set obj = ActiveSheet.Shapes.AddShape(msoShapeRightTriangle, 200, 200, [f5], [e5])
End Sub