+ Reply to Thread
Results 1 to 6 of 6

Can I draw triangles using VBA

  1. #1
    Registered User
    Join Date
    03-20-2013
    Location
    Yorkshire
    MS-Off Ver
    Excel 2010
    Posts
    3

    Can I draw triangles using VBA

    I know this has been done before but just to keep my mind active in retirement I have decided to program excel to calculate the properties of a triangle given three sides or two sides and an angle etc. etc.

    I now want to draw that triangle on the worksheet - I want it to be the correct shape. It is easy to calculate the co-ordinates of the three corners in terms of X and Y as you would plot on graph paper.

    What I am looking for is how to translate that into vba code. Code like this

    With myDocument.Shapes.AddLine(10, 10, 250, 250).Line

    would appear to be one way of doing it but how would I convert the co-ordinates of the triangle into "points" that the above command requires. It is possible to define the origin that the "points" use or is it always in the top left corner of the work sheet.

    AQ

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Can I draw triangles using VBA

    One way would be to calculate start end end points and use them.
    Please Login or Register  to view this content.
    Add a starting minimum for each left and top. (Values in certain Cells maybe)
    Select all three and group if you want to make a single shape.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Can I draw triangles using VBA

    Another way.
    Cell N1 has a starting point from left.
    Cell O1 has a starting point from the top
    Cell I10 has the length from left to right
    Cell J10 has the length up and down


    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Can I draw triangles using VBA

    Are you required to use a shape or drawing type of object? I would be inclined to do this kind of thing on an XY scatter chart.

    1) Calculate the X,Y coordinates of the triangle's corners from the triangle properties.
    2) Insert a scatter chart using the X and Y ranges (remember that you need to have the starting point also at the end in order to close the triangle).
    3) Format the scatter chart the way you want it to look.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    03-20-2013
    Location
    Yorkshire
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Can I draw triangles using VBA

    Thank you for the suggestions. I will look at them both and see which will do what I want the best. I would never have thought of using a chart for this.

    Re the idea

    Sub MacroTest()
    ActiveSheet.Shapes.AddLine(250#, 200#, 300#, 300#).Name = "Side1"
    ActiveSheet.Shapes.AddLine(200#, 300#, 300#, 300#).Name = "Side2"
    ActiveSheet.Shapes.AddLine(250#, 200#, 200#, 300#).Name = "Side3"
    End Sub

    That is more or loess what I was planning on using but I then came across the problem of converting the triangle co-ordinates say (0,0) (0, 12), (8.5, 13.25) into points to use in this formula. O could not find a procedural way of doing this only trial and error. I will have one point at the origin, one side on th ex axis and one somewhere above. As in my example. The point o, o will be somewhere in the middle of the spread sheet.

    I am progressing with this and will have to do a bit more work



    AQ

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Can I draw triangles using VBA

    Re: The point o, o will be somewhere in the middle of the spread sheet.
    No, everything start from the left of your sheet (=0) and the top of your sheet (=0)
    The suggestion to add a starting minimum for each left and top. (Values in certain Cells maybe) will start from both these values.
    Your 0, 0 will start from these values.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Strange Green Triangles help
    By m77wls in forum Excel General
    Replies: 3
    Last Post: 07-22-2015, 05:00 AM
  2. Green Triangles in Cells
    By bigroo1958 in forum Excel General
    Replies: 4
    Last Post: 05-12-2014, 04:11 PM
  3. [SOLVED] Green triangles for no reason (I know of)
    By sperrysperry in forum Excel General
    Replies: 6
    Last Post: 04-02-2014, 09:26 PM
  4. Odd triangles/arrows next to cells
    By Xee in forum Excel General
    Replies: 4
    Last Post: 10-01-2007, 05:47 PM
  5. little green triangles - XL 2003
    By dpenny in forum Excel General
    Replies: 2
    Last Post: 09-30-2005, 04:05 PM
  6. Mass removal of green triangles
    By Jonathan in forum Excel General
    Replies: 2
    Last Post: 05-12-2005, 05:06 AM
  7. Little Green Triangles.
    By Derrick Fawsitt in forum Excel General
    Replies: 2
    Last Post: 01-12-2005, 02:06 PM

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