Modifying a Donut Chart

TheTiredEngineer

New Member
Joined
Jul 31, 2024
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Ive got a donut chart in excel with some data. Im trying to adjust the FirstSliceAngle depending on a user input. I keep trying this code and I keep getting an error message at
Excel Formula:
 pieChart.FirstSliceAngle = angleValue

What am I doing wrong here?

Excel Formula:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim chartObj As ChartObject
    Dim pieChart As Chart
    Dim ws As Worksheet
    Dim angleCell As Range
    Dim angleValue As Integer

    ' Set the worksheet and the cell to monitor for angle changes
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Adjust the sheet name
    Set angleCell = ws.Range("A1") ' Specify the cell where the angle will be input

    ' Check if the changed cell is the one we're monitoring
    If Not Intersect(Target, angleCell) Is Nothing Then
        ' Get the new angle value from the cell
        angleValue = angleCell.Value
        
        ' Ensure the value is between 0 and 360
        If angleValue < 0 Or angleValue > 360 Then
            MsgBox "Please enter a value between 0 and 360."
            Exit Sub
        End If
        
        ' Set the chart object to "Chart 1"
        On Error Resume Next ' Prevent errors if the chart is not found
        Set chartObj = ws.ChartObjects("Chart 1")
        On Error GoTo 0 ' Resume normal error handling
        
        ' Check if the chart exists and is a pie or donut chart
        If Not chartObj Is Nothing Then
            Set pieChart = chartObj.Chart
            If pieChart.ChartType = xlPie Or pieChart.ChartType = xlDoughnut Then
                ' Set the FirstSliceAngle property to the value from the monitored cell
                ' Set the FirstSliceAngle property on the chart object, not the PlotArea
                
                On Error GoTo handleError
                pieChart.FirstSliceAngle = angleValue
                Exit Sub
                
                MsgBox "FirstSliceAngle set to " & angleValue & " for Chart 1"
            Else
                MsgBox "Chart 1 is not a pie or donut chart."
            End If
        Else
            MsgBox "Chart 1 not found."
        End If
    End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I have a weak internet here, so will not do a deeper search, but I think that ChartObj.Chart does not have FirstSliceAngle. I think it is a level deeper, probably in ChartGroup so try first member of chartgroups collection:
VBA Code:
pieChart.ChartGroups(1).FirstSliceAngle = angleValue
 
Upvote 0
Solution
I have a weak internet here, so will not do a deeper search, but I think that ChartObj.Chart does not have FirstSliceAngle. I think it is a level deeper, probably in ChartGroup so try first member of chartgroups collection:
VBA Code:
pieChart.ChartGroups(1).FirstSliceAngle = angleValue
Thanks, I found and kept trying that but kept getting an error. This worked this time.
 
Upvote 0
Glad to hear so, and thanks for marking my post as a solution
 
Upvote 0

Forum statistics

Threads
1,221,527
Messages
6,160,342
Members
451,638
Latest member
MyFlower

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