VBA图表范围

时间:2017-12-14 13:34:27

标签: vba charts

如何绘制图表范围?

A保留值的名称, B“日期,不需要绘制”, C,E,D是整数,如1,2,3等, 当我尝试这个代码时,它也会绘制未选中的“B”列。

Dim rango1, rango2 As String
rango1 = "A2:A3"
rango2 = "C2:E3"
ActiveSheet.Shapes.AddChart.Select

ActiveChart.SetSourceData Source:=Range(rango1, rango2)

2 个答案:

答案 0 :(得分:0)

这对我有用:

Option Explicit

Sub TestMe()        
    Dim rango1 As Range, rango2 As Range
    Dim django As Range
    Set rango1 = Range("A2:A3")
    Set rango2 = Range("C2:E3")
    Set django = Union(rango1, rango2)
    ActiveSheet.Shapes.AddChart.Select        
    ActiveChart.SetSourceData Source:=Range(django.Address)    
End Sub

确实,如果你写的话很奇怪

ActiveChart.SetSourceData Source:=Range(rango1, rango2)

它也将在中间选择列B。希望Excel的开发人员有他们的理由。

答案 1 :(得分:0)

对我而言以下

Sub makechart()

Dim rango1 As String, rango2 As String
rango1 = "A2:A3"

rango2 = "C2:E3"

ActiveSheet.Shapes.AddChart.Select

Dim myStr As String

myStr = rango1 & "," & rango2

ActiveChart.SetSourceData Source:=Range(myStr) 

End Sub