Add new series on a graph using a macro

aikue

New Member
Joined
Aug 8, 2014
Messages
10
Hello everyone. I am trying to create a macro. I am very inexperienced and my knowledge of coding is very minimal. I would be very grateful if anyone could help me out. Thank you :)

This is a copy of my table (bold are the rows and coloums). The workbook is called "PSD Graphs". The Sheet is called "Cone Crusher PSD."

I want to enter data on a weekly basis, adding more rows as the year progresses. I want to make a macro so I can select a date and add its row of data onto my graph.

BAEAFAGAHAO
5905031.5250
2363-jul55.97.73.42.10
23711-jul40.28.3110
23824-jul84.920.33.63.10
23930-jul36.813.90.80.60

<tbody>
</tbody>

I tried recording a macro and this is what I get:

Sub Macro11()
'
' Macro11 Macro
'
' Keyboard Shortcut: Ctrl+m
'
ActiveSheet.ChartObjects("Chart 5").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = "='Cone Crusher PSD'!$B$238"
ActiveChart.SeriesCollection(2).XValues = "={90,50,31.5,25}"
ActiveChart.SeriesCollection(2).Values = "='Cone Crusher PSD'!$AE$238:$AO$238"
End Sub


When I select a date and try to apply my macro, it adds a blank series to my graph.

Thank you again for any help!!
 
aikue,

I think you need to reference the .SeriesCollection() differently. When I substitute this code:
Code:
[COLOR=#0000ff]Sub[/COLOR] Macro11()
[COLOR=#008000]'
' Macro11 Macro
'
' Keyboard Shortcut: Ctrl+m
'[/COLOR]
[COLOR=#0000ff]With [/COLOR]ActiveSheet.ChartObjects("Chart 5").Chart
[COLOR=#0000ff]Set[/COLOR] ns = .SeriesCollection.NewSeries
ns.Name = "='Cone Crusher PSD'!$B$238"
.SeriesCollection(Sheets("Cone Crusher PSD").Range("B238").Value).XValues = "={90,50,31.5,25}"
.SeriesCollection(Sheets("Cone Crusher PSD").Range("B238").Value).Values = "='Cone Crusher PSD'!$AE$238:$AO$238"
[COLOR=#0000ff]End With
End Sub[/COLOR]

For the below code. The procedure runs without errors:

Code:
[COLOR=#0000ff]Sub [/COLOR]Macro11()


[COLOR=#0000ff]     With [/COLOR]ActiveSheet.ChartObjects("Chart 5").Chart
   [COLOR=#0000ff]      Set[/COLOR] ns = .SeriesCollection.NewSeries
         ns.Name = "='Cone Crusher PSD'!$B$238"
         .SeriesCollection(1).XValues = Array(90, 50, 31.5, 25)
         .SeriesCollection(1).Values = "='Cone Crusher PSD'!$AE$238:$AO$238"
[COLOR=#0000ff]     End With[/COLOR]


[COLOR=#0000ff]End Sub[/COLOR]
The problem with this is that it will only act on the first series in a chart. The series just added might be the second or third series, etc.

Chris
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Have a great weekend Chris!

Mickle, that macro does run without errors; however, when I select the date and click "run," a new series is created on the graph, always with the name 24-Jul, and without any x or y values. Interesting.
 
Upvote 0
aikue / Chris Mack,

Not sure what your data looks like aikue but, I would go with Chris Mack's alteration. I executed his procedure and it and it seems to run without errors. I think it's what you want to use. I am not familiar with .SeriesCollection so I wasn't sure what the
(1) was doing, but I knew that this portion of that larger line of code: (Sheets("Cone Crusher PSD").Range("B238").Value) was causing the error. Good fix Chris!
 
Upvote 0
Thanks, good to know it's working.

Strange that my original SeriesCollection statement didn't work as it works in another macro I wrote. I'll have to check it out more and see what the difference is.
 
Upvote 0
Sorry I disappeared for a while. Strange it worked for you Mickle; I still get error '1004' Invalid Parameter with Chris' modification:

Sub Macro11()
'
' Macro11 Macro
'
' Keyboard Shortcut: Ctrl+m
'
With ActiveSheet.ChartObjects("Chart 5").Chart
Set ns = .SeriesCollection.NewSeries
ns.Name = "='Cone Crusher PSD'!$B$238"
.SeriesCollection(CStr(ns.Name)).XValues = "={90,50,31.5,25}"
.SeriesCollection(CStr(ns.Name)).Values = "='Cone Crusher PSD'!$AE$238:$AO$238"
End With
End Sub

I still get the bug on ".SeriesCollection(CStr(ns.Name)).XValues = "={90,50,31.5,25}"

How would I upload an excel file?
 
Upvote 0
You can upload it to Box.com or something, then post the link here.

(It's free to make an account.)

Chris.
 
Upvote 0
I'm confused as to why it didn't work, but here's a pretty good solution:

Code:
Sub Macro11()
'
' Macro11 Macro
'
' Keyboard Shortcut: Ctrl+m
'
With ActiveSheet.ChartObjects("Chart 5").Chart
n = .SeriesCollection.Count
Set ns = .SeriesCollection.NewSeries
ns.Name = "='Cone Crusher PSD'!$B$238"
.SeriesCollection(n + 1).XValues = "={90,50,31.5,25}"
.SeriesCollection(n + 1).Values = "='Cone Crusher PSD'!$AE$238:$AO$238"
End With
End Sub
Hope this helps,

Chris.
 
Upvote 0
Chris! That works so much better :D!

The only problem is that it only graphs the one date for Jul 24th, B238. When I click on another date, and apply the macro, it graphs July 24th's data "$AE$238:$AO$238", instead of the data on the new row.

Is there a way of selecting a new date, say cell B237, applying the macro, and adding a new series on the graph with data
AE$237:$AO$237?

Thanks,

Is there any way I can repay you for your help? I appreciate the help tremendously. I really want to learn how to write macros myself. Do you know of any good online lessons or programs?


-Aikue
 
Upvote 0

Forum statistics

Threads
1,215,581
Messages
6,125,656
Members
449,247
Latest member
wingedshoes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top