Excel vba图表,编辑范围

时间:2012-03-14 14:11:34

标签: vba excel-vba excel

Sub StoragevsQuota()
    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Sheets("Data").Range("E1:G32")
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Storage Charts"
    ActiveChart.Parent.Name = "Used Space vs Disk Quota"
    ActiveChart.SetElement (msoElementChartTitleCenteredOverlay)
    ActiveChart.ChartTitle.Text = "Used Space vs Disk Quota"
End Sub

在excel vba中,只需点击E1:G32范围内的按钮,即可为我生成图表。但这是一个静态范围

我希望保持大致相同的语法,但是它要循环到E列,例如一直向下直到没有更多的数据,所以它将是32,然后将该范围扩展到G32

它的列长度可以因图形而变化而不会产生多少列,任何人都可以帮我设置它,就像我上面所说的读取列直到没有数据输入然后扩展范围3个列以上的案例?

谢谢Z

Sub WeeklySuccessOrFailure() 
'On Error Resume Next
Dim lastRow As Long
With Sheets("Data")
lastRow = .range("AA" & Rows.Count).End(xlUp).Row
Charts.Add
ActiveChart.SetSourceData Source:=Sheets("Data").range("AA1:AA & lastRow, AD1:&      lastRow, AE1:AE & lastRow")
ActiveChart.ChartType = xlColumnClustered
ActiveChart.Location Where:=xlLocationAsObject, Name:="Job Charts"
ActiveChart.Parent.Name = "Total Weekly Success or Failure"
ActiveChart.SetElement (msoElementChartTitleCenteredOverlay)
ActiveChart.ChartTitle.Text = "Total Weekly Success Or Failure Of Jobs"
End With
End Sub

4 个答案:

答案 0 :(得分:2)

这是你在尝试什么? (的 UNTESTED

Sub StoragevsQuota()
    Dim lastRow As Long

    With Sheets("Data")
        lastRow = .Range("G" & Rows.Count).End(xlup).Row

        Charts.Add
        ActiveChart.ChartType = xlColumnClustered
        ActiveChart.SetSourceData Source:=.Range("E1:G" & lastRow)
        ActiveChart.Location Where:=xlLocationAsObject, Name:="Storage Charts"
        ActiveChart.Parent.Name = "Used Space vs Disk Quota"
        ActiveChart.SetElement (msoElementChartTitleCenteredOverlay)
        ActiveChart.ChartTitle.Text = "Used Space vs Disk Quota"
    End With
End Sub

<强>后续

  
    
      @siddarth只是为了引起你的注意你有任何机会可以帮助我解决我在下面的评论中发布的问题boschiero评论,谢谢 - Zenaphor 8秒前

    
  

这是你在尝试什么? (再次出现

ActiveChart.SetSourceData Source:=Sheets("Data").Range("AA1:AA" & lastRow & _
",AD1:AD" & lastRow & ",AE1:AE" & lastRow)

答案 1 :(得分:1)

就个人而言,我更喜欢我的范围的开始和结束单元格是动态的,所以我的代码将是这样的:

ActiveChart.SetSourceData Source:=.Range(.cells(1,5),.cells(7,lastRow))

但实际上只是因为我更喜欢指定范围的单元符号。 Siddharth的回答非常明显。

对于多个非连续范围,我将创建一个Range变量,然后使用Union来指定我想要的任何范围。

所以让我说我想要列AA,AD和AE,就像Siddharth的例子一样,我会做以下事情:

Sub WeeklySuccessOrFailure() 
'On Error Resume Next
Dim lastRow As Long
Dim myRange as Range
With Sheets("Data")
lastRow = .range("AA" & Rows.Count).End(xlUp).Row
Charts.Add
Set myRange = Union(.range(.cells(1,27),cells(lastRow,27)),_
.range(.cells(1,30),.cells(lastRow,30)),_
.range(.cells(1,31),.cells(lastRow,31)))
ActiveChart.SetSourceData Source:= myRange
ActiveChart.ChartType = xlColumnClustered
ActiveChart.Location Where:=xlLocationAsObject, Name:="Job Charts"
ActiveChart.Parent.Name = "Total Weekly Success or Failure"
ActiveChart.SetElement (msoElementChartTitleCenteredOverlay)
ActiveChart.ChartTitle.Text = "Total Weekly Success Or Failure Of Jobs"
End With
End Sub

我没有测试上面的代码,但我之前使用过myRange和Union方法。

我会说它比Siddharth的建议有点笨拙,但我发现它更容易用于自动化目的。如果你不打算自动化宏的部分找到要绘制的列,即它总是相同的列,那么Siddharth的方法更容易,也可能更直观。

答案 2 :(得分:0)

如果数据是一个漂亮的矩形,由空行和列包围,你可以很容易。选择数据范围中的单元格,然后运行代码,该代码查找包含活动单元格的整个数据区域:

Sub MakeChartFromIndicatedRange1()
  Dim rng As Range
  Set rng = ActiveCell.CurrentRegion
  Charts.Add
  ActiveChart.SetSourceData Source:=rng
  ' etc.
End Sub

或者,如果用户要选择整个范围,则此代码使用所选区域:

Sub MakeChartFromIndicatedRange2()
  Dim rng As Range
  Set rng = Selection
  Charts.Add
  ActiveChart.SetSourceData Source:=rng
  ' etc.
End Sub

或者完全模仿Excel的内置范围定义器,如果它是多个单元格则使用所选范围,或者如果只选择一个单元格则包含活动单元格的整个区域。:

Sub MakeChartFromIndicatedRange()
  Dim rng As Range
  If Selection.Cells.Count = 1 Then
    Set rng = ActiveCell.CurrentRegion
  Else
    Set rng = Selection
  End If
  Charts.Add
  ActiveChart.SetSourceData Source:=rng
  ' etc.
End Sub

答案 3 :(得分:0)

我的工作示例:

注意:Grafik2 是一个包含行号的数组,例如 14、22、30。我的 Excel 表格是这样的:

行号、名称(A 列)、值(D 列)

...

14,姓名 1,85

...

22,姓名 2,75

...

30,姓名 3,82

Dizisi = ""
For i = 1 To GrafikSay
    Dizisi = Dizisi + "A" + Trim(Str(Grafik2(i))) + ":D" + Trim(Str(Grafik2(i)))
    If i <> GrafikSay Then
        Dizisi = Dizisi + "; " 'in some countries this should be comma ", "
    End If
Next i
oBook.Charts.Add
oXL.activechart.SetSourceData Source:=oSheet.range(Dizisi)