运行宏时,工作簿崩溃 - 创建图表

时间:2018-01-15 09:04:28

标签: vba excel-vba excel

我遇到了一个宏,我无法找到解决方案。 我需要创建一个饼图,然后我为它创建了一个子程序,但是当我调用这个宏时,excel会崩溃。 但是,如果我使用开发人员选项卡并使用F8按钮,它不会崩溃。 任何的想法? 这是代码:

Sub create_pie_chart()

    Dim ws1, ws2 As Worksheet
    Dim r, c As Integer

    Dim path, pathopti, main_file, thismonth, lymonth As String
        path = ThisWorkbook.path
        pathraw = path & "\raw\"
        pathopti = path & "\optimized\"
        main_file = path & "Main_reporting.xlsm"
        thismonth = ThisWorkbook.Sheets("Main dashboard").Cells(3, 5).Value
        lymonth = ThisWorkbook.Sheets("Main dashboard").Cells(4, 5).Value

    Set ws1 = ActiveWorkbook.Worksheets("World Family")
    thismonth = ThisWorkbook.Sheets("Main dashboard").Cells(3, 5).Value
    r = Range(Range("A1"), Range("A1").End(xlDown)).Rows.count

    Application.ScreenUpdating = False

    ActiveSheet.Shapes.AddChart2(251, xlPie).Select

    With ActiveChart
        .SetSourceData Source:=Sheets("World Family").Range("A3:B" & r), PlotBy:=xlColumns
        .ClearToMatchStyle
        .ApplyLayout (2)
        .ChartStyle = 259
        .ChartTitle.Text = "Values for " & thismonth
        .SeriesCollection(1).Explosion = 10
    End With

    ActiveSheet.Shapes.AddChart2(251, xlPie).Select
    With ActiveChart
        .SetSourceData Source:=Sheets("World Family").Range("A3:A" & r & ",F3:F" & r), PlotBy:=xlColumns
        .ClearToMatchStyle
        .ApplyLayout (2)
        .ChartStyle = 259
        .ChartTitle.Text = "Units for " & thismonth
        .SeriesCollection(1).Explosion = 10
    End With

    Application.ScreenUpdating = True

End Sub

0 个答案:

没有答案