如何使用 VBA 以编程方式修改嵌入式条形图/柱形图?

发布于 2024-10-07 20:21:50 字数 901 浏览 5 评论 0原文

我正在创建一个 ProcessBook 显示,该显示用一组条目填充嵌入式 Microsoft Office 11.0 电子表格对象 (Office 2003)。然后我计算有关它们的汇总数据;此汇总数据不应以电子表格形式在屏幕上显示,但确实需要用于生成条形图。该数据当前用于填充单独的 Microsoft Office 11.0 电子表格对象。它的组织方式使得每个条形图的标题位于 A 列中,相应的值位于 B 列中。

由于这是 ProcessBook,我什至在访问嵌入对象时遇到了一些困难,但我已经成功嵌入并获得了访问 ChartSpace 对象以及子 ChChart 对象。不幸的是,我不知道如何手动设置条形的值或如何使用 .SetData.SetSpreadsheetData 方法将其指向一个对象我已经住满了

访问 ChartSpace 对象非常简单:ThisDisplay.ChartSpace1
然后我可以添加图表并相当轻松地访问它:
<代码> 将 objChart 调暗为 ChChart
设置 objChart = ThisDisplay.ChartSpace1.Charts.Add(0)

我也可以轻松访问我的电子表格值:
<代码> strBarName = ThisDisplay.sstChartData.Range("A2").Value
intBarVal = ThisDisplay.sstChartData.Range("B2").Value

如何实际设置数据源或手动设置 ChChart 对象中的条形值?或者,如何使用不同的对象来实现相同的目标?

I'm creating a ProcessBook display that populates an embedded Microsoft Office 11.0 Spreadsheet object (Office 2003) with a set of entries. I'm then calculating aggregate data about them; this aggregate data should not be visible in spreadsheet form onscreen, but does need to be used to generate a bar chart. The data is currently being used to populate a separate Microsoft Office 11.0 Spreadsheet object. It's organized such that the title of each bar chart is in column A and the corresponding value is in column B.

Since this is ProcessBook, I've had some difficulty even gaining access to embedded objects, but I've managed to embed and gain access to a ChartSpace object, as well as a child ChChart object. Unfortunately, I can't figure out how to either manually set the values of the bars or how to use the .SetData or .SetSpreadsheetData methods to point it to an object that I've populated.

Accessing the ChartSpace object is pretty straightforward: ThisDisplay.ChartSpace1
I can then add a Chart and access it fairly easily:

Dim objChart as ChChart
Set objChart = ThisDisplay.ChartSpace1.Charts.Add(0)

I can access my spreadsheet values pretty easily as well:

strBarName = ThisDisplay.sstChartData.Range("A2").Value
intBarVal = ThisDisplay.sstChartData.Range("B2").Value

How do I actually set the data source or manually set the values of the bars in the ChChart object? Alternatively, how do I use a different object to accomplish the same goal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-10-14 20:21:50

显然坚持是这里的关键。我设法确定如何手动添加值以及如何引用现有电子表格对象。这两个示例都大量取自在线 ChChart 文档;我想当我第一次尝试时我只是累了,并且一定在某个地方输错了一些东西。

<代码>

Public Sub AddValuesToChartManually()  
Dim objChart As ChChart  
With ThisDisplay.ChartSpace1
    Do While .Charts.Count > 0
        ThisDisplay.ChartSpace1.Charts.Delete (0)
    Loop

    Set objChart = .Charts.Add
    objChart.HasTitle = True
    objChart.Title.Caption = "Chart Title"
    objChart.Axes.Item(0).HasTitle = True
    objChart.Axes.Item(1).HasTitle = True
    objChart.Axes.Item(0).Title.Caption = "Axis 0"
    objChart.Axes.Item(1).Title.Caption = "Axis 1"
    objChart.HasLegend = False        

    Dim astrHeaders(0 To 4) As String
    Dim aintValues1(0 To 4) As String
    Dim aintValues2(0 To 4) As String
    Dim astrSeries1(0) As String
    Dim astrSeries2(0) As String

    astrHeaders(0) = "AL1"
    astrHeaders(1) = "AL2"
    astrHeaders(2) = "AL3"
    astrHeaders(3) = "AL4"
    astrHeaders(4) = "AL5"

    astrSeries(0) = "Series Name"

    aintValues(0) = 1
    aintValues(1) = 3
    aintValues(2) = 17
    aintValues(3) = 1
    aintValues(4) = 7

    objChart.Type = .Constants.chChartTypeColumnClustered

    Call objChart.SetData(chDimSeriesName, .Constants.chDataLiteral, astrSeries)
    Call objChart.SetData(chDimCategories, .Constants.chDataLiteral, astrHeaders)
    Call objChart.SeriesCollection(0).SetData(.Constants.chDimValues, .Constants.chDataLiteral, aintValues)

End With  
End Sub


Public Sub AddValuesFromSpreadsheet()
Dim objChart1 As ChChart

With ThisDisplay.ChartSpace1
    Do While .Charts.Count > 0
        ThisDisplay.ChartSpace1.Charts.Delete (0)
    Loop

    Set .DataSource = ThisDisplay.sstChartData

    Set objChart1 = .Charts.Add
    ' Set the chart type.
    objChart1.Type = .Constants.chChartTypeColumnClustered

    ' Display titles
    objChart1.HasTitle = True
    objChart1.Title.Caption = "Chart Title"

    ' Bind the series name to cell B1 in the first sheet of the spreadsheet
    objChart1.SetData chDimSeriesNames, .Constants.chDataBound, "B1"

    ' Bind the category axis to cell A2:A28 in the first sheet of the spreadsheet.
    objChart1.SetData chDimCategories, .Constants.chDataBound, "A2:A6"

    ' Bind the values of the data series to cells B2:B28 in the first sheet of the spreadsheet.
    objChart1.SeriesCollection(0).SetData chDimValues, .Constants.chDataBound, "B2:B6"

End With
End Sub

<代码>

Apparently persistence is the key here. I managed to determine how to both add values manually and how to refer to the existing spreadsheet object. Both examples take heavily from the online ChChart documentation; I guess I was just tired when I attempted it in the first place and must have mistyped something somewhere.

Public Sub AddValuesToChartManually()  
Dim objChart As ChChart  
With ThisDisplay.ChartSpace1
    Do While .Charts.Count > 0
        ThisDisplay.ChartSpace1.Charts.Delete (0)
    Loop

    Set objChart = .Charts.Add
    objChart.HasTitle = True
    objChart.Title.Caption = "Chart Title"
    objChart.Axes.Item(0).HasTitle = True
    objChart.Axes.Item(1).HasTitle = True
    objChart.Axes.Item(0).Title.Caption = "Axis 0"
    objChart.Axes.Item(1).Title.Caption = "Axis 1"
    objChart.HasLegend = False        

    Dim astrHeaders(0 To 4) As String
    Dim aintValues1(0 To 4) As String
    Dim aintValues2(0 To 4) As String
    Dim astrSeries1(0) As String
    Dim astrSeries2(0) As String

    astrHeaders(0) = "AL1"
    astrHeaders(1) = "AL2"
    astrHeaders(2) = "AL3"
    astrHeaders(3) = "AL4"
    astrHeaders(4) = "AL5"

    astrSeries(0) = "Series Name"

    aintValues(0) = 1
    aintValues(1) = 3
    aintValues(2) = 17
    aintValues(3) = 1
    aintValues(4) = 7

    objChart.Type = .Constants.chChartTypeColumnClustered

    Call objChart.SetData(chDimSeriesName, .Constants.chDataLiteral, astrSeries)
    Call objChart.SetData(chDimCategories, .Constants.chDataLiteral, astrHeaders)
    Call objChart.SeriesCollection(0).SetData(.Constants.chDimValues, .Constants.chDataLiteral, aintValues)

End With  
End Sub


Public Sub AddValuesFromSpreadsheet()
Dim objChart1 As ChChart

With ThisDisplay.ChartSpace1
    Do While .Charts.Count > 0
        ThisDisplay.ChartSpace1.Charts.Delete (0)
    Loop

    Set .DataSource = ThisDisplay.sstChartData

    Set objChart1 = .Charts.Add
    ' Set the chart type.
    objChart1.Type = .Constants.chChartTypeColumnClustered

    ' Display titles
    objChart1.HasTitle = True
    objChart1.Title.Caption = "Chart Title"

    ' Bind the series name to cell B1 in the first sheet of the spreadsheet
    objChart1.SetData chDimSeriesNames, .Constants.chDataBound, "B1"

    ' Bind the category axis to cell A2:A28 in the first sheet of the spreadsheet.
    objChart1.SetData chDimCategories, .Constants.chDataBound, "A2:A6"

    ' Bind the values of the data series to cells B2:B28 in the first sheet of the spreadsheet.
    objChart1.SeriesCollection(0).SetData chDimValues, .Constants.chDataBound, "B2:B6"

End With
End Sub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文