Modifying a Donut Chart

TheTiredEngineer

New Member
Joined
Jul 31, 2024
Messages
31
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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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,223,929
Messages
6,175,461
Members
452,645
Latest member
Tante

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