Chart Object Error 91

  • Thread starter Thread starter Bob Barnes
  • Start date Start date
B

Bob Barnes

I'm an Access Programmer, but was asked why this proceduire fails w/ Error
91...
I tried setting ..Dim ActiveChart As ChartObject...but it still fails...see
where I have
"<--- Where Error 91 occurs" below. TIA - Bob

Sub ColorBySeriesName()
Dim rPatterns As Range
Dim iSeries As Long
Dim rSeries As Range

Set rPatterns = ActiveSheet.Range("AR6:BH235")

With ActiveChart
For iSeries = 1 To .SeriesCollection.Count <--- Where Error 91 occurs
Set rSeries = rPatterns.Find(What:=.SeriesCollection(iSeries).Name)
If Not rSeries Is Nothing Then
.SeriesCollection(iSeries).Interior.ColorIndex = _
rSeries.Interior.ColorIndex
End If
Next
End With
End Sub
 
Works fine when you actually have a chart selected. Probably the user did
not have a chart selected, hence "ActiveChart" is not defined. That's what
error 91 is telling you.

HTH,

Eric
 
Bob

Replace

<<With ActiveChart
by

With ActiveSheet.ChartObjects(1).Chart
(if you have only one chart)

OR

With ActiveSheet.ChartObjects("chartname").Chart
(if you have only one chart)

If this post helps click Yes
 
Thank you. Just tried...

....snippet...Down...
Dim ActiveChart As ChartObject

Set ActiveChart = ActiveSheet.ChartObjects("Chart 16")
Set rPatterns = ActiveSheet.Range("AR6:BH235")

With ActiveChart
For iSeries = 1 To .SeriesCollection.Count <-- Still Error 91 here

....snippet...Up
 
Bad idea!

ActiveChart is a built-in Excel object name and refers to the currently
selected chart on a sheet. You should not use that same name for your
variable. Call it something else, like "MyChart" or whatever, just not
ActiveChart.

Did you try just selected a chart on the current worksheet and then running
your original code? If that doesn't work, try Jacob's suggestion.

HTH,

Eric
 
Oops. I should have removed the comment "(if you have only one chart)
" from the second option. If you have more more number of charts; specify
the name..

With ActiveSheet.ChartObjects(1).Chart
(if you have only one chart)

OR

With ActiveSheet.ChartObjects("Chart 16").Chart
(if you have multiple chart objects)

If this post helps click Yes
 
With ActiveSheet.ChartObjects("Chart 16").Chart
....solved the problem..

Thank you & EricG - Bob
 
With ActiveSheet.ChartObjects("Chart 16").Chart
....solved the problem..

Thank you & Jacob - Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top