【问题标题】:VBA - BoxPlot diagram in Excel 2016VBA - Excel 2016 中的箱线图
【发布时间】:2018-03-27 08:20:22
【问题描述】:

我目前在 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

第一个图表的创建没有任何问题。对于第二个图表,Excel 报错:“对象不支持此操作。”

上线:

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

这是 BoxWhisker 图的问题吗?还是我合并了一个错误

谢谢!

最好的问候, 蒂莫

【问题讨论】:

  • 您能否说明一小部分输入数据的预期输出?您可以使用table generator 格式化数据,然后在代码标签之间添加。
  • @QHarr 我可以做到。我很惊讶代码不起作用,因为我用 Excel 记录了它
  • 欢迎来到宏记录器的世界!恐怕这可能发生在宏录制的代码中。这并不罕见。
  • |测试1 | 2 | -3 | 1 | 2 | |:-----:|---|----|----|----| |测试2 | 3 | -4 | -2 | -1 | |测试3 | 5 | 6 | 4 | 2 |
  • @QHarr 它应该是这样的:boxplot

标签: excel vba diagram boxplot


【解决方案1】:

这可以简化,但对于初学者来说,这是正确的想法吗?

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

有测试数据

【讨论】:

  • 箱须图通常有很多缺陷。也许该元素不适用于那个
  • 是的。折线图再次起作用,但箱线图不包含任何数据
  • 也许我的 Excel 版本对于 BoxPlot 图表来说太旧了? ://
  • 这是 Excel 2016,我对语法的更正似乎有效。 :-( 你运行的是什么版本?
  • 该死的。我真的不知道为什么这对我不起作用:(
猜你喜欢
  • 2019-03-03
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多