使用命名范围VBA的多行

时间:2017-06-06 08:03:38

标签: excel vba excel-vba excel-charts

我试图将多个命名范围绘制到图表上。这些命名范围(以例如Time_Series_1命名)具有以下形式:
=INDIRECT(Ranges!$AJ$3)此参数Ranges!$AJ$3例如是

 ="'Source Data'!"&ADDRESS(N3,(Comparisons!$G$28+5))&":"&ADDRESS(N3(Comparisons!$I$28+5))

我试图用来绘制这些范围的代码(我已经开始使用其中的一个,因为一个人不能工作,因此不可能):

Sheets("Comparisons").Select
Sheet10.ChartObjects("Chart 2").Activate

Set Rng = range("Time_Series_1")
Set Date_Rng = range("Time_Series_Dates_1")

With ActiveChart
    .SetSourceData Source:=Rng
    .XValues = Date_Rng
End With

它不能正常工作!图表仍为空白。完全空了。我没有更新什么?我没有激活什么?

谢谢

2 个答案:

答案 0 :(得分:0)

我无法测试你的模型,但试试这个:

Dim Chart_Sheet As Worksheet
Dim My_Chart As ChartObject
Dim Rng As Range
Dim Date_Rng As Range

Set Chart_Sheet = ThisWorkbook.Sheets("Comparisons")
Set My_Chart = Chart_Sheet.ChartObjects("Chart 2")
Set Rng = Range("Time_Series_1")
Set Date_Rng = Range("Time_Series_Dates_1")

With My_Chart.Chart
    .SetSourceData Source:=Rng
    .SeriesCollection(1).XValues = Date_Rng
End With

(我假设您的间接参考正常工作)

答案 1 :(得分:0)

 ActiveChart.SeriesCollection(1).XValues = Date_Rng
 ActiveChart.SeriesCollection(1).Values = Rng
 ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
 ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Dates"
 ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
  ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Pounds"
 ActiveChart.Axes(xlCategory).HasMajorGridlines = True