excel - Plotting a line graph with a dynamic number of data rows -
i trying plot line graph on summary sheet based on dynamic number of lines on weight tracking sheet (i.e. add row of data every day)
i trying make line graph example (same colors etc.). right now, code compiles graph doesn't appear anywhere.
here spreadsheet https://drive.google.com/file/d/0b1glubx-ronhsnjpddrftfvubda/edit?usp=sharing
private sub weighttrackingchart() 'variable declaration dim long dim lastrow long dim wtchart shape dim ws worksheet 'find last used row lastrow = sheets("weight tracking").range("b3").end(xlup).row 'looping fifth row till last row has data = 6 lastrow 'prints chart summary sheet set ws = sheets("weight tracking") 'deletes old charts ' ws.shapes("wtchart").delete 'adds new chart sheet set wtchart = ws.shapes.addchart(xlline, 15, 750, 500, 400) 'sets chart name wtchart.name = "wtchart" 'now line chart added...setting data source here sheets("weight tracking") wtchart.setsourcedata source:=.range(.cells(i, 1), .cells(i, "f")) end next end sub
first off i'm not huge fan of using cells(x,y), it's personal issue see have written how feel comfortable feel free adjust please. appears though loop creating many charts instead of adding new series existing. have changed loop begins. additionally, activating chart allow use of activechart in code. not know way around this, if else awesome learn.
another thing note future use, make sure looping properly. when setting loop run until last row saying run 7 times instead of 5. also, should length down go data, not times loop (series create). length take difference between first row , last row , tell offset far down.
anyways, hope helps you. worked wonderfully me.
private sub weighttrackingchart() 'variable declaration dim long dim lastrow long dim wtchart shape dim ws worksheet 'find last used row lastrow = sheets("weight tracking").range("b3").end(xldown).row length = lastrow - sheets("weight tracking").range("b3").row 'prints chart summary sheet set ws = sheets("weight tracking") 'deletes old charts ' ws.shapes("wtchart").delete 'adds new chart sheet set wtchart = ws.shapes.addchart(xlline, 15, 150, 500, 400) 'sets chart name wtchart.name = "wtchart" 'now line chart added...setting data source here sheets("weight tracking") 'looping fifth row till last row has data = 0 4 wtchart.select activechart.seriescollection.newseries activechart.seriescollection(i + 1).name = .range("b2").offset(0, + 1) activechart.seriescollection(i + 1).values = range(.range("b2").offset(1, + 1), .range("b2").offset(length + 1, + 1)) activechart.seriescollection(i + 1).xvalues = range(.range("b3"), .range("b3").offset(length, 0)) next end end sub
ps major kudos posting spreadsheet on google drive, made life easier.
Comments
Post a Comment