简体   繁体   中英

VBA - BoxPlot diagram in Excel 2016

I'm currently having a problem creating a boxplot chart in Excel

temp.Activate

With ActiveSheet
'First diagram
.Shapes.AddChart2(227, xlLine).Select

With ActiveChart
.SetSourceData Source:=ActiveSheet.Range(ActiveSheet.Cells(2, 1), ActiveSheet.Cells(pEnd, 15))
.ChartTitle.Text = "Diagram 1"
.Axes(xlValue).MinimumScale = -30
.Axes(xlValue).MaximumScale = 25
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Axe1"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Axe2"
.Axes(xlValue).Crosses = xlMaximum
.HasLegend = True
ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleWidth 3, msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleHeight 2, msoFalse, msoScaleFromTopLeft
.ClearToMatchStyle
.ChartStyle = 230
End With

.Shapes.AddChart2(408, xlBoxwhisker, 200, 100, 350, 200, True).Select
With ActiveChart
.SetSourceData Source:=ActiveSheet.Range(ActiveSheet.Cells(2, 1), ActiveSheet.Cells(pEnd, 15))
.ChartTitle.Text = "Diagram 2"
.HasLegend = True
ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleWidth 3, msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleHeight 2, msoFalse, msoScaleFromTopLeft
End With 'Chart


End With 'temp

The first diagram is created without any problems. For the second chart, Excel reports the error: "Object does not support this action."

On the line:

.SetSourceData Source:=ActiveSheet.Range(ActiveSheet.Cells(2, 1), 
ActiveSheet.Cells(pEnd, 15))

Is this a problem of the BoxWhisker diagram? Or did I incorporate an error

Thank you!

Best regards, Timo

This can be simplified but for starters is it the right idea?

Option Explicit
Sub TEST()

    Dim temp As Worksheet
    Set temp = ThisWorkbook.Worksheets("Sheet1")

    Dim pEnd As Long

    pEnd = 3

    temp.Activate

    With ActiveSheet
        'First diagram
        .Shapes.AddChart2(227, xlLine).Select

        With ActiveChart
            .SetSourceData Source:=ActiveSheet.Range(ActiveSheet.Cells(2, 1), ActiveSheet.Cells(pEnd, 15))
            .ChartTitle.Text = "Diagram 1"
            .Axes(xlValue).MinimumScale = -30
            .Axes(xlValue).MaximumScale = 25
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Axe1"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Axe2"
            .Axes(xlValue).Crosses = xlMaximum
            .HasLegend = True
            ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleWidth 3, msoFalse, msoScaleFromTopLeft
            ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleHeight 2, msoFalse, msoScaleFromTopLeft
            .ClearToMatchStyle
            .ChartStyle = 230
        End With

        With .Shapes.AddChart2(408, xlBoxwhisker, 200, 100, 350, 200, True)

            With ActiveChart
                .SetSourceData Source:=ActiveSheet.Range(ActiveSheet.Cells(2, 1), ActiveSheet.Cells(pEnd, 15))

                .ChartTitle.Text = "Diagram 2"
                .HasLegend = True
                ActiveSheet.Shapes(.Parent.Name).ScaleWidth 3, msoFalse, msoScaleFromTopLeft
                ActiveSheet.Shapes(.Parent.Name).ScaleHeight 2, msoFalse, msoScaleFromTopLeft
            End With

        End With                                 'Chart


    End With                                     'temp

End Sub

With test data

测试数据

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM