我有一个宏,它运行生成一个新工作表并用各种图表填充它。这些图表的数据来自sheet2。我希望能够在生成图表后删除sheet2,而不会丢失图表中的所有信息。到目前为止,我已尝试将计算设置为手动、保护工作表以及使用 ProtectData 和 ProtectSelection 方法保护图表。以上都失败了。有没有办法使用 vba 来阻止图表生成后发生变化?谢谢。
编辑: 一个答案建议用一定范围内的值填充数组,然后使用该数组生成图表。现在,我在尝试生成图表时遇到了类型不匹配错误。是否可以将源数据设置为等于数组?到目前为止,这是我的代码:
Dim PlotRangeBar() As Long
Dim PlotRangePie As Variant
Dim XValPie As Variant
Dim XRangeBar As Range
Dim CellCount As Long
'Create bar graph
Set XRangeBar = ActiveWorkbook.Sheets(2).Range("B" & DataStart & ":B" & DataEnd)
i = 0
For Row = DataStart To DataEnd
If Cells(Row, UsedColTimesheet).FormulaR1C1 <> "0" And Cells(Row, UsedColTimesheet) <> vbNullString Then
ReDim Preserve PlotRangeBar(i)
PlotRangeBar(i) = Cells(Row, UsedColTimesheet).Value
i = i + 1
End If
Next
ActiveWorkbook.Sheets(Sheets.Count).Select
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.ChartType = xlColumnStacked
.SetSourceData Source:=PlotRangeBar
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = XRangeBar
.SetElement (msoElementChartTitleCenteredOverlay)
.ApplyLayout (1)
.ChartTitle.Text = ResourceName & " - Hours per project"
.Legend.Delete
.ChartStyle = 18
.ProtectSelection = True
End With
最佳答案
将数据加载到数组中,并在创建图表时使用数组加载图表。
关于vba - 如何阻止Excel中的图表自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31323024/