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

时间:2010-12-14 09:45:42

标签: vba charts ms-office spreadsheet

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

由于这是ProcessBook,我在访问嵌入对象方面遇到了一些困难,但我设法嵌入并获得了对ChartSpace对象以及子ChChart对象的访问权限。不幸的是,我无法弄清楚如何手动设置条形值或如何使用.SetData.SetSpreadsheetData方法将其指向我已填充的对象。

访问ChartSpace对象非常简单:ThisDisplay.ChartSpace1
然后我可以添加一个图表并轻松访问它:
Dim objChart as ChChart
Set objChart = ThisDisplay.ChartSpace1.Charts.Add(0)

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

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

1 个答案:

答案 0 :(得分:0)

显然,持久性是关键所在。我设法确定如何手动添加值以及如何引用现有的电子表格对象。这两个例子都非常重视在线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

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