多图表创建系列重载

时间:2018-04-17 14:56:38

标签: vba excel-vba excel

从我的大量数据创建多个图表时遇到了问题。我需要创建4个单独的图表。成功创建第一个图表后,我的下一个图表的代码类似,但代码在下面的图表中产生了一个不计算数量的系列。我的第二个图表共有6个系列,第3个图表有33个系列,第4个图表有48个。在我的数据选择中,每个图表列B是我的日期,然后第二个范围是我的数据,但在我的2-4中图表代码开始选择我未指定的列。我该如何解决/解决这个问题?

我的Sub用于创建这些图表:

Sub ChartC()


Dim EmbChart As Chart, EmbChart1 As Chart, EmbChart2 As Chart, EmbChart3 As 
Chart
Dim cht As Object
Dim wks As Worksheet
Dim LastRow As Long

For Each wks In Worksheets

If wks.ChartObjects.Count > 0 Then
  wks.ChartObjects.Delete
End If
Next wks


With Application.ActiveSheet
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With


'Cement Inventory Graph
Set EmbChart = ActiveSheet.Shapes.AddChart(Left:=2760, Width:=1000, 
Top:=150, Height:=400).Chart


With EmbChart
    .SetSourceData Source:=Sheets("TestSheet_Const_Ktonnage").Range("B5:C" & LastRow)
    .Type = xlLine
    .HasTitle = True
    .ChartTitle.Text = "Year Overview of Total Cement Inventory"
    .SeriesCollection(1).Name = "Total Cement Inventory"
End With


'Clinker Inventory Graph
Set EmbChart1 = ActiveSheet.Shapes.AddChart(Left:=2760, Width:=1000, Top:=650, Height:=400).Chart


With EmbChart1
    .SetSourceData Source:=Range("B5:B" & LastRow, "H5:H" & LastRow)
    .Type = xlLine
    .HasTitle = True
    .ChartTitle.Text = "Year Overview of Clinker Inventory"
End With


'Operational Efficiency Kiln1
 Set EmbChart2 = ActiveSheet.Shapes.AddChart(Left:=2760, Width:=1000, Top:=1150, Height:=400).Chart


With EmbChart2
    .SetSourceData Source:=Range("B5:B" & LastRow, "AI5:AI" & LastRow)
    .Type = xlLine
    .HasTitle = True
    .ChartTitle.Text = "Year Overview of Operational Efficiency For Kiln1"
End With


'Operational Efficiency Kiln2
Set EmbChart3 = ActiveSheet.Shapes.AddChart(Left:=2760, Width:=1000, Top:=1650, Height:=400).Chart


With EmbChart3
    .SetSourceData Source:=Range("B5:B" & LastRow, "AX5:AX" & LastRow)
    .Type = xlLine
    .HasTitle = True
    .ChartTitle.Text = "Year Overview of Clinker Inventory"
End With

End Sub

1 个答案:

答案 0 :(得分:1)

使用逗号分隔的范围编写非相邻列。因此,不要给Range两个参数,而是给它一个以逗号分隔的范围参数:

Source:=Range("B5:B" & LastRow & "," & "AI5:AI" & LastRow)

因此,如果LastRow为150,则缩小为

Source:=Range("B5:B150,AI5:AI150")

希望有所帮助