Results 1 to 9 of 9

How to draw lines on an excel chart programmatically

Threaded View

  1. #1
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    How to draw lines on an excel chart programmatically

    I have an excel chart (Stock candlestick chart) which I created manually.
    I draw lines on the chart (say a horizontal line from (x1=1 to x2=4 along y = 27)
    You can see the red horizontal line in the screen shot below
    Candle.png

    Though for a quick screenshot purpose, I have shown a column chart above, I actually want to do it on a stock-candlestick chart.

    I am comfortable with writing excel functions which do other stuff. I am also able to call these functions from a cell =MyFunction(10,20). But unable to figure out how to do this for a sub/function/macro which draws a line on the chart.

    From the docs, it seems I need a sub/function which does something like this
    Sub DrawLine()
    ActiveSheets.ChartObjects(1).Shapes.AddLine(1, 27, 4, 27)
    End Sub
    But I have a few problems

    1) I want to the co-ordinates of the lines to be points on the chart (x & y co-ordinates as per the chart axis) - however AddLine seems to be absolute geometric points on the chart rather than points which can be represented by x,y co-ordinates as per the chart axes.

    2) I am unable to figure how to call this DrawLine so as to draw the line. If I call it from the Run Menu of the VBAProject, I get a

    Run Time error 24 - object required.

    So how do I call this? In the future, I will also pass parameters to the sub so as to draw multiple unconnected lines which don't pass through or connect the candle itself. How do I do this? I am experienced programmer but only have a basic experience with MS Office programming.
    Last edited by ExcelBoy90; 12-17-2019 at 06:09 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Excel VBA : Chart : How do you determine, programmatically the chart range
    By TMS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-03-2016, 09:50 AM
  2. Hi need help to draw a line chart having two lines of data
    By Sabyasachi_Samal in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 08-11-2016, 04:24 PM
  3. How to draw static vertical lines in Excel graph
    By vivekengg in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 05-03-2011, 10:04 AM
  4. Replies: 6
    Last Post: 08-16-2008, 10:24 AM
  5. Draw lines in a chart
    By ravichander in forum Excel Charting & Pivots
    Replies: 33
    Last Post: 07-12-2008, 08:31 AM
  6. Replies: 1
    Last Post: 04-07-2005, 10:06 PM
  7. how do i draw a chart with excel
    By bkhojolita in forum Excel General
    Replies: 1
    Last Post: 04-01-2005, 08: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