Create PieChart => Error 91

witchcraftz

New Member
Joined
Jan 17, 2008
Messages
39
I have a code where I am pulling data from a sheet and then creating Pie charts based on the data.

My proof of concept code uses the following code and works - but is too simple since I need to create 3 charts from each line:

Code:
With ws            
            .Shapes.AddChart.Select
            ActiveChart.SetSourceData Source:=Range(.Name & "!" & "D" & iRow & ":E" & iRow)
            ActiveChart.ChartType = xlPie
            
            ActiveChart.Location Where:=xlLocationAsObject, Name:="PieCharts"
            
End With

So I have created the following code that dynamically changes the range but it keeps giving me error 91 on the "ActiveChart.SetSourceData Source:=Range" line.

Code:
With ws
                txtSource = .Name & "!" & ConvertToLetter(iCol + 16) & iRow & ":" & ConvertToLetter(iCol + 17) & iRow                
                .Shapes.AddChart.Select
                ActiveChart.SetSourceData Source:=Range(txtSource)
                ActiveChart.ChartType = xlPie
                
                ActiveChart.Location Where:=xlLocationAsObject, Name:=Sheet3.Name '"PieCharts"


End With

Keep in mind that this is a sample and that in reality I have 500+ lines with 3 data columns on which to create pie charts.

Here is the sample workbook:

https://drive.google.com/open?id=1bFEBIvq8IwONk5jv9DeyC7rLHEnu_Nsk
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
This works for me:

Code:
Sub CreateChartsFinal()
Dim iRow As Integer, iCol%, ws As Worksheet, Shp As Shape, i%
Dim txtSource As String, txtChartName As String, txtChartTitle$
Set ws = Sheet4 'Sheets("Data")
ws.Activate
For iCol = 1 To 3 Step 2
    For iRow = 2 To 3
        With ws
            txtSource = ConvertToLetter(iCol + 16) & iRow & ":" & ConvertToLetter(iCol + 17) & iRow
            txtChartTitle = .Range("G" & iRow).Text & " " & .Range(ConvertToLetter(iCol + 16) & "1").Text
            txtChartName = "ChartRow" & iRow & "Col" & iCol
            .Shapes.AddChart.Select
            With ActiveChart
                .SetSourceData Sheets(ws.Name).Range(txtSource)
                .ChartType = xlPie
                .HasTitle = True
                .ChartTitle.Text = txtChartTitle
                .Parent.Cut
            End With
            Sheet3.Paste
        End With
Next iRow, iCol
Set ws = Nothing
For i = 1 To Sheet3.ChartObjects.Count      ' organize charts
    Sheet3.ChartObjects(i).Top = i * 14
    Sheet3.ChartObjects(i).Left = i * 18
Next
End Sub
 
Upvote 0
Excellent! I hadn't come across the idea of cut+paste into the other sheet for this before but it makes sense.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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