简体   繁体   中英

Excel 2010 VBA Deletes Chart

I support an Excel VBA application in my spare-time. 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.

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.

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.

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.

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. 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.

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.

The issue arose when using VBA code to copy worksheet elemetnts to PowerPoint. Was copying as printer, bitmat using:

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.

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.

Thanks to all who helped.

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