Consulting

Results 1 to 3 of 3

Thread: auto draw triangle from cell values in worksheet

  1. #1

    auto draw triangle from cell values in worksheet

    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.
    triangle.PNG

    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.

  2. #2
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    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

  3. #3
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •