简体   繁体   English

Excel 2010 VBA删除图表

[英]Excel 2010 VBA Deletes Chart

I support an Excel VBA application in my spare-time. 我在业余时间支持Excel VBA应用程序。 I am manufacturing process engineer, not a professional developer. 我是制造工艺工程师,而不是专业开发人员。

The charting portions of the applicaiton have worked without issue in Excel 2007. My company is upgrading to Excel 2010 and that same application now has problems with the interaction with the charts on worksheets. 应用程序的图表部分在Excel 2007中没有问题。我的公司正在升级到Excel 2010,同一应用程序现在与工作表上的图表交互存在问题。

Thie issue is with a bar chart. 这个问题是条形图。 The application, when reseting, deletes the series from the chart using the code below. 重置时,应用程序使用以下代码从图表中删除系列。 I did this so that while new data is being imported / processed there isn't a time where a chart is presented to the user that is not up-to-date with the latest data. 我这样做是为了在导入/处理新数据时,没有时间向用户呈现与最新数据不同步的图表。

        'select the histogram chart
        Sheets(sChartSheet).Select
        ActiveSheet.ChartObjects("Chart 15").Activate

        Call PBarCaption("Delete Existing Histogram Series")
        'remove any existing series
        For i = 1 To ActiveChart.SeriesCollection.Count
            ActiveChart.SeriesCollection(1).Delete
        Next i

then creates new series as new data to be charted is imported from an external data file: 然后创建新系列,因为要从外部数据文件导入要绘制的新数据:

    'add series for histogram
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Values = "=HistogramData!$B$5:$B$29"
    ActiveChart.SeriesCollection(1).XValues = "=HistogramData!$A$5:$A$29"
    ActiveChart.SeriesCollection(1).charttype = xlColumnClustered

The issue with 2010 is that occasionally when the VBA code is ran, the entire bar-chart is 'lost' (deleted) rather than the chart being present without any defined series. 2010年的问题是偶尔运行VBA代码时,整个条形图“丢失”(删除)而不是没有任何已定义系列的图表。

The 'lost' chart seems to happen when concsecutive executions of the base code is performed via another section of VBA code that automatically creates a series of charts and copies them to PowerPoint. 当通过VBA代码的另一部分执行基本代码的连续执行时,“丢失”图表似乎发生,该部分自动创建一系列图表并将它们复制到PowerPoint。

When the sequence of charts is executated manually, it works without issue. 当手动执行图表序列时,它可以正常工作。 When ran automatically, on the generation of the second chating sequence, the chart is getting deleted. 当自动运行时,在生成第二个聊天序列时,图表将被删除。

I am hoping that someone is familiar with the changes in the charting from Excel 2007 to 2010 version and will be able to help. 我希望有人熟悉从Excel 2007到2010版本的图表更改,并且能够提供帮助。

Thanks, 谢谢,

Len 莱恩

It might help to rework your code so you're not activating/selecting and then depending on the "active" object not changing: that can break if some other code activates some other object when you're not expecting it... 它可能有助于修改代码,因此您不会激活/选择,然后根据“活动”对象不改变:如果某些其他代码在您不期望它时激活某些其他对象,则可能会中断...

Dim cht As Chart

Set cht = ActiveWorkbook.Sheets(sChartSheet).ChartObjects("Chart 15").Chart
Do While cht.SeriesCollection.Count > 0
    cht.SeriesCollection(1).Delete
Loop

With cht.SeriesCollection
    .NewSeries
    With .Item(1)
        .Values = "=HistogramData!$B$5:$B$29"
        .XValues = "=HistogramData!$A$5:$A$29"
        .ChartType = xlColumnClustered
    End With
End With

I reformated all interactions with the chart to use the "with" rather than selecting. 我重新格式化了与图表的所有交互,以使用“with”而不是选择。 Its better programming. 它更好的编程。

Unfortunately that didn't solve the problem. 不幸的是,这并没有解决问题。

The comments regarding the Print Spooler were helpful and it looks like the issue is related to the interaction with how charts appear on the screen and the printer. 关于Print Spooler的评论很有帮助,看起来这个问题与图表在屏幕和打印机上的显示方式的交互有关。

Reference 参考

As many other users within my organization have different printers and when trying the MS Office print drivers as default didn't improve the restuls another solution had to be found. 由于我组织中的许多其他用户使用不同的打印机,并且在尝试MS Office打印驱动程序时默认情况下没有改进恢复,因此必须找到另一种解决方案。

The issue arose when using VBA code to copy worksheet elemetnts to PowerPoint. 使用VBA代码将工作表元素复制到PowerPoint时出现此问题。 Was copying as printer, bitmat using: 使用以下方式复制为打印机,bitmat:

rSlideArea.CopyPicture (2)  

Changed to copying as screen bitmap using: 更改为使用以下方式复制为屏幕位图:

rSlideArea.CopyPicture Appearance:=xlScreen, Format:=xlBitmap

Interestingly resulting PowerPoint files with bitmap formatting were easier to work with and were smaller. 有趣的是,带有位图格式的PowerPoint文件更易于使用并且更小。

The result is that the pictures in PowerPoint aren't as 'pretty' with screen formatting but the application now works in Excel v2010 which was the immediate priority. 结果是PowerPoint中的图片不像屏幕格式那样漂亮,但应用程序现在可以在Excel v2010中使用,这是当务之急。

Thanks to all who helped. 感谢所有帮助过的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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