+ Reply to Thread
Results 1 to 16 of 16

Draw triangle that changes size depending on cell values.

Hybrid View

  1. #1
    Registered User
    Join Date
    06-27-2010
    Location
    London, England
    MS-Off Ver
    Excel 2000; soon, 2007
    Posts
    4

    Draw triangle that changes size depending on cell values.

    Wouldn't know what VBA was if it knocked down and killed me, lumbered with Openoffice at home, so, if at all possible, could you post an worksheet with the solution embedded? I am genuinely sorry, and will learn VBA, just not this weekend.
    ----------
    Excel 2000; w2000p
    ---------
    I'd like to draw a right-angled triangle, anchored [not changing position within the worksheet] on the right angle, which changes side length depending on cell values entered for two or three of [height, base, hypotenuse].
    So, for values 120 and 20, in cells a1 and a2, need to produce a 120*20 pt triangle, area 1200sqpts.

    I am aware of ways to do something similar using conditional formatting, but it's the triangle I need for a printed infographic.

    I am also aware that you can resize a triangle manually/use charts. However this is impractical given that the output is a diagram with a grid of 48 triangles, expected to alter fast depending on variables elsewhere in the spreadsheet, and charts need resizing anyway to preserve scale.

    Many thanks for your time.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Draw triangle that changes size depending on cell values.

    How about this?

    Enter the horizontal and vertical sides in A1 and A2.
    (The length of the hypotenuse is automatically calculated in A3)
    Enter the scale of the drawing in A5.
    Press the button and the triangle will be drawn by this sub
    Sub makeTriangle()
        Dim Horiz As Double, Vert As Double
        Dim cornerTop As Double, cornerLeft As Double
        On Error GoTo Halt
        With ThisWorkbook.Sheets("Sheet1")
            Horiz = Val(CStr(.Range("A1").Value)) * Val(CStr(.Range("A5").Value))
            Vert = Val(CStr(.Range("A2").Value)) * Val(CStr(.Range("A5").Value))
            cornerTop = .Range("D2").Top
            cornerLeft = .Range("D2").Left
            
            With .Shapes("Line 1"): Rem horizontal
                .Top = cornerTop
                .Left = cornerLeft
                .Height = 0
                .Width = Horiz
            End With
            
            With .Shapes("Line 2"): Rem vertical
                .Top = cornerTop
                .Left = cornerLeft
                .Height = Vert
                .Width = 0
            End With
            
            With .Shapes("Line 3"): Rem hypotinuse
                .Top = cornerTop
                .Left = cornerLeft
                .Height = Vert
                .Width = Horiz
            End With
        End With
    Halt:
        On Error GoTo 0
    End Sub
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    06-27-2010
    Location
    London, England
    MS-Off Ver
    Excel 2000; soon, 2007
    Posts
    4

    Re: Draw triangle that changes size depending on cell values.

    Many thanks for this. I've had a play around, and while it's great for drawing one triangle, it doesn't seem to want to draw two.

    The two ways I've tried it are:
    1)
    Adding a separate macro, Maketriangle1 (), and executing in a group macro. In this instance, it leaves only the three lines with the three lowest x values [1,2,3] of the .Shapes("Line x") arguments.

    The funny thing is, you can see Excel drawing both triangles before it wipes one away. I've tried messing about with the Static command, but it still leaves just one triangle drawn.

    I renamed all of the defined variables and cell references. Can the .Shapes() command only draw one thing at once?

    2)
    Same as above, but adding more commands under the same macro. Now it draws nothing!

    Also, is there any way to add a fill color/colour to the shape?

    Many thanks for your help.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Draw triangle that changes size depending on cell values.

    Excel is not a graphics package. If you have a fixed number of triangles, they can be created with the Draw capabilities. But if for creating and moving them on the fly, Excel is not the program for the task.

  5. #5
    Registered User
    Join Date
    06-27-2010
    Location
    London, England
    MS-Off Ver
    Excel 2000; soon, 2007
    Posts
    4

    Re: Draw triangle that changes size depending on cell values.

    I'm not wanting to move them, or change the number of them at on. I want to alter their sizes on the fly [I have Illustrator for drawing things with].

    My problem is just with creating a fixed number of triangles greater than one. I haven't been able to draw two working triangles given my adaptations to the code, kindly supplied previously.

    Many thanks,

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Draw triangle that changes size depending on cell values.

    If you could post a copy of your workbook, it might help me understand what you are looking for.
    If you have existing triangles, calculating the coordinates of the end-points is pretty straight forward.
    Applying those coordinates to the triangles is heavily dependent on how your triangles are made (3 line segments or a closed curve or ... ) and how they are named.

  7. #7
    Registered User
    Join Date
    06-27-2010
    Location
    London, England
    MS-Off Ver
    Excel 2000; soon, 2007
    Posts
    4

    Re: Draw triangle that changes size depending on cell values.

    Thanks, attached.
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: Draw triangle that changes size depending on cell values.

    Hi

    To draw a right triangle you can also use the RightTriangle shape.

    For ex.:

    Sub DrawRightTriangle(sName As String, dLeft As Double, dTop As Double, dWidth As Double, dHeight As Double)
    Dim shp As Shape
    
    Set shp = ActiveSheet.Shapes.AddShape(msoShapeRightTriangle, dLeft, dTop, dWidth, dHeight)
    shp.Name = sName
    End Sub

    A test to create your 120*20 triangle

    DrawRightTriangle "RightTriangle01", 50, 100, 20,120

    You can use a loop that calls DrawRightTriangle to create your 48 triangles.
    You know their name so they will be easy to manipulate.

    To fill the shape with a color you can add to the procedure, for ex.:

    shp.Fill.ForeColor.RGB = RGB(255, 0, 0)

  9. #9
    Registered User
    Join Date
    08-24-2012
    Location
    Stuttgart, Germany
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Draw triangle that changes size depending on cell values.

    Hi folks!

    Long time since the last entry, but anyway...
    I'm new here and have been following the thread, because I pretty much need to automatically draw a triangle as the original request of the post.
    There are some differences though:
    - I just need 1 triangle per macro (or maybe two, but I guess that's not important right now).
    - It's not a right triangle. It's a oblique triangle, and I have all the length values, no need to write formulas.

    I have downloaded the "workbook1" that mikerickson gently posted, and I thought I could easily transform it to my needs, but I'm not going any further (Oh yeah, I'm totally new to vba, but I can follow some logical instructions...). Here are the difficulties I found:
    - I was not able to change the "orientation" of the triangle, if you know what I mean. As the original triangle from the post kind of has an orientation which begins at the Top/Left corner and grows to the right and to the bottom, my triangle has its origin in the Bottom/Left corner, and grows to th right and to the top. I've attached and image which I hope will help you understand.
    veltri.JPG

    So, basically, that's my problem, because I'm not able to "change the origin" of the triangle. I've tried some weird lines like "corner.Bottom" and stuff, but I guess that doesn't exist, right....

    I'm sorry for my unappropriate terms, but I would really appreciate a help or a hint on what should I do.
    Thank you!

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Draw triangle that changes size depending on cell values.

    Rosswell,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  11. #11
    Registered User
    Join Date
    08-24-2012
    Location
    Stuttgart, Germany
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Draw triangle that changes size depending on cell values.

    Hi Arlette,

    I'm really sorry for the mistake. I posted in the same thread because I didn't want to make a fuzz about it, hehe..
    Sorry, won't happen again. What will happen, on the other hand, is me reading the forum rules properly...

    Cheers!

  12. #12
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Draw triangle that changes size depending on cell values.

    In reply to the request for a trapezoid version of this:

    Note the position and size entries in columns J and K.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    04-04-2016
    Location
    pakistan
    MS-Off Ver
    2007
    Posts
    15

    Re: Draw triangle that changes size depending on cell values.

    Dear all i want to create a free from or polygon shapes by cell inputs.as the values enters it create shape as per cell measurments in inches or cm .i did it by hiding/showing pics.but i want to draw a true shape as per given values to see effect.please view excel sheet for understanding

    http://www.excelforum.com/attachment...7&d=1461123964
    Attached Files Attached Files

  14. #14
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953

    Re: Draw triangle that changes size depending on cell values.

    riz4u,
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Ben Van Johnson

  15. #15
    Registered User
    Join Date
    04-04-2016
    Location
    pakistan
    MS-Off Ver
    2007
    Posts
    15

    Re: Draw triangle that changes size depending on cell values.

    DEAR I AM NEW I DIDNT GETTING RIGHT RESPONSE. I POST MY ARTICLE BUT IT DISAPEAR .http://www.excelforum.com/attachment...7&d=1461123964

  16. #16
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,914

    Re: Draw triangle that changes size depending on cell values.

    riz4u, your post did not disappear. It is here.

    In your post above, you have pasted a truncated link. I am not going to get into a big explanation of what that means here, seeing as though you have violated the hijacking rule. Please continue in your other thread, not this one, as protonLeah has pointed out.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1