+ Reply to Thread
Results 1 to 14 of 14

Changing the width and line type of some (not all) lines in a graph

  1. #1
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Changing the width and line type of some (not all) lines in a graph

    I have a graph that is loaded with data by a VBA code. Each time I run the code, new data is added to the graph and the graph regenerates automatically to display that data. So far, so good.

    The problem is that, when the graph regenerates, it sets various line weights back to the default width and back to solid line (they had been dashes).

    When I look-up online how to set the line width, I find lots of posts that describe how to set all of the lines in a graph to any width you want - but I want different lines to have different widths and for the narrow lines to also be dashed (instead of a solid line). So, the solutions I find online to set all of the lines to the same width are not sufficient for me.

    Can anyone tell me how to make the width of some lines on a graph thinner and dashed, but leave other lines in the same line at the default width and line type (i.e., a continuous line)?

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Changing the width and line type of some (not all) lines in a graph

    Does this help get you further?

    Please Login or Register  to view this content.
    https://peltiertech.com/conditional-...art-using-vba/
    Last edited by jeffreybrown; 05-11-2019 at 11:36 AM.
    HTH
    Regards, Jeff

  3. #3
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Re: Changing the width and line type of some (not all) lines in a graph

    Thank you.

    How (other than by trial and error) can I tell which line is in SeriesCollection(1), which line is SeriesCollection(2), etc.?

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Changing the width and line type of some (not all) lines in a graph

    I'm not by a computer right now, but I'd say it has to do with the order they are in when the data is setup!

  5. #5
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Re: Changing the width and line type of some (not all) lines in a graph

    I was hoping that, instead of SeriesCollection(1), I could use something like SeriesCollection("Name of the line's legend"); i.e., something that uniquely identified each line.

    I'd say it has to do with the order they are in when the data is setup!
    This would be a reasonable approach to try, except I'm not sure what "the order they are in when the data is setup" means. Does this mean the order in which I created each line? (Because I created four at once) Does this mean the order in the legend? (What happens when I add a new line and the order in the legend changes?) Etc.

    So, trial and error is how I'll identify the lines.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Changing the width and line type of some (not all) lines in a graph

    I don't think naming the collection is what you do here. The collection is a series of numbers and they are dictated by the legend. Even if you create all of the lines at once, the first column of data is usually the Horizontal axis, then the next column collection (1), the next collection (2), and so on.

    Look at this example. There is column A, the Horizontal Axis, column B, collection (1) and so on.

    Open up the VBE and step thru the code. Watch the chart and see what happens. Often times, simply stepping thru your code can help pinpoint any problems areas which can easily lead to the fix.

    To step thru your code, place the cursor inside the procedure and select F8. Now continue to press F8 to see the entire code execute.

    But yes, in the end, trial and error is how we all started out learning.

    (What happens when I add a new line and the order in the legend changes?)
    I suspect you will have to update your code, but why add the new line out of order. Simple move it up or down and then all you have to do is add one additional collection series. If you already have (5), make the new line (6) and add to your existing code.

    I don't think you can names lines like you would create named ranges, but then again, maybe so. I've been proved wrong on this forum many times before.
    Attached Files Attached Files
    Last edited by jeffreybrown; 05-11-2019 at 06:20 PM.

  7. #7
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Re: Changing the width and line type of some (not all) lines in a graph

    Although I tried to build on the previous posts and looked for related information online, I haven't been able to figure out how to address the elements of any graph on the worksheet in the attached file. Would someone please show me the VBA code for the attached spreadsheet to make, on the graph located around D730, the horizontal lines 1.25 pt. wide and the line type 'continuous dash'? If your feeling ambitious, I would appreciate you adding the code to make the rising line green and the falling line red.
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Re: Changing the width and line type of some (not all) lines in a graph

    On reading more articles on graphs, I've discovered:
    • Excel calls a 'graph' a 'chart'
    • When I create a chart from the insert ribbon, it is an embedded chart
    • When I create a chart from a VBA command, it is referenced differently than a chart I create from the insert ribbon

    Given the above information, I can clarify that the chart I want to modify is embedded.

    Still not noticeably closer to a solution.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,972

    Re: Changing the width and line type of some (not all) lines in a graph

    Just FYI, you can refer to the series by name with SeriesCollection; you don't have to use a number.
    Everyone who confuses correlation and causation ends up dead.

  10. #10
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Re: Changing the width and line type of some (not all) lines in a graph

    After a lot of pointless reading (I never found a good overview on how to modify embedded charts), it occurred to me to record a macro of my modifying the graph that the spreadsheet produces into the spreadsheet that I wanted to have. I could then run the macro immediately after the graph is regenerated, with the result that the graph will maintain the appearance I want.

    It still took many attempts to create a macro that properly modifies the graph, but I eventually did. I now call that macro as the first line of code following the code that updates the graph. The macro follows:
    Please Login or Register  to view this content.

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Changing the width and line type of some (not all) lines in a graph

    Thanks Rory for the nudge.

    @colorado, After doing some research based on Rory's nudge, try this where you can name the collection.

    Please Login or Register  to view this content.
    On my small test, this seems to work, but more testing to fit your needs might be required.

    Here is a link I found.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,972

    Re: Changing the width and line type of some (not all) lines in a graph

    For a normal chart, rather than a Chartgroup as was the case in that SO question, all you need is the name. No need to use CVar, or indeed Cstr.

  13. #13
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Changing the width and line type of some (not all) lines in a graph

    Thanks Rory.

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,972

    Re: Changing the width and line type of some (not all) lines in a graph

    Thank you for publicising my SO answer.

+ 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. Changing Chart line default width
    By SolomonTadesse in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 09-12-2013, 02:12 PM
  2. Replies: 1
    Last Post: 03-23-2012, 10:17 PM
  3. Changing line-width in a line chart with a lot of series
    By LJ25 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 10-13-2011, 11:23 PM
  4. Maximum Width of Lines in Line Graph
    By neutrino in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 12-05-2007, 04:28 PM
  5. How do i get lines on a line graph?
    By baffledbrewer in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 06-04-2006, 08:38 AM
  6. How do i get lines on a line graph?
    By baffledbrewer in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 06-04-2006, 08:31 AM
  7. [SOLVED] Line Graph with 2 lines
    By Need help making line graph with 2 lines in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 05-19-2006, 03:50 PM
  8. Changing the Width of Bars in a Horizontal bar Graph
    By Ott1 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 06-22-2005, 04:05 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