VBA Loop to fill Chart

ExcelKid_10

Board Regular
Joined
Mar 17, 2004
Messages
87
hello-

I have a simple code that I wrote to loop through data line by line to create a column chart. The code stalls out on the .Values line where I try when I try to add the first series....I get a run-time error '1004'. however, after searching numerous times I cannot find what might be causing it....here is my code

Sub UpdateGraphs_NEW()
Dim LastRow As Long
Dim irow, icol As Long
Dim strSourceSheet As String
Dim strGraphSheet As String

strSourceSheet = "NewGraphData"
strGraphSheet = "Graphs"


'Find the last row of data
Sheets(strSourceSheet).Select
LastRow = Range("o65536").End(xlUp).Row
LastRow = LastRow - 2


Sheets(strGraphSheet).ChartObjects("Chart 4").Activate

For i = 1 To LastRow
irow = i + 2
With ActiveChart.SeriesCollection.NewSeries
.Name = Sheets(strSourceSheet).Cells(irow, 15)
.Values = Sheets(strSourceSheet).Range(Cells(irow, 19), Cells(irow, 24))
.XValues = Sheets(strSourceSheet).Range(Cells(3, 48), Cells(8, 48))
End With
Next i

End Sub

Just looking for someone to point me in the right direction...

thanks!!!
EK
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
This problem followed me around forever until I got it straight in my head. Your statement here looks very logical:

.Values = Sheets(strSourceSheet).Range(Cells(irow, 19), Cells(irow, 24))

But you can't use Range and Cells together like that in all cases. When you use Cells, remember it is not a stand-alone entity, it is actually the child of other objects, like Range.Cells or Worksheet.Cells. Just because Excel lets you type it by itself doesn't mean it is not associating itself with something.

Just like Range, if you type Cells with nothing in front of it it will assume you mean the active worksheet.

So, if you are on worksheet A, and your code says: Sheets("B").Range(Cells(1,1)......
you will get an error, because you started with a range on sheet B, and then tried to merge it with cell ranges on sheet A. Make sense?

Anyways, long story short, fully qualify your Cell references and see if that works.

.Values = Range(Sheets(strSourceSheet).Cells(irow, 19), Sheets(strSourceSheet).Cells(irow, 24))
 
Upvote 0
...and of course:
Code:
    .XValues = Range(Sheets(strSourceSheet).Cells(3, 48), Sheets(strSourceSheet).Cells(8, 48))
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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