使用范围的问题,对于堆积柱形图

时间:2016-05-31 07:42:07

标签: excel vba

我正在尝试创建一个瀑布图(xlColumnStacked),其中rngToSelect作为要使用的数据。但是,当我尝试运行宏时,我收到 “方法范围的对象_global失败” 错误。任何人都可以看到我收到此错误的原因?

我在顶部声明rngToSelect,然后在我添加源数据时调用它。

Sub Waterfall()
'
' Waterfall Macro
'

'
Dim rngData As Range
Dim intCounter As Integer
Dim rngToSelect As Range

Set rngData = ActiveCell.CurrentRegion

Set rngToSelect = Range(rngData.Cells(1, 1), rngData.Cells(rngData.Rows.Count, 1))
For intCounter = 1 To rngData.Columns.Count
    If rngData.Cells(1, intCounter).Value <> "Values" Then
        Set rngToSelect = Union(rngToSelect, Range(rngData.Cells(1, intCounter), rngData.Cells(rngData.Rows.Count, intCounter)))
    End If
Next intCounter

rngToSelect.Select

ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range(rngToSelect)
    ActiveChart.ChartType = xlColumnStacked


End Sub

1 个答案:

答案 0 :(得分:0)

我通过以下方式解决了这个问题:

而不是: ActiveChart.SetSourceData Source:=Range(rngToSelect)

我做了:

ActiveChart.SetSourceData Source:=rngToSelect