来自VBA的一张图表中不同表格的多个情节

时间:2016-08-31 12:49:20

标签: vba excel-vba excel-2010 excel

我的问题是我想在一张图表中制作许多图表,但数据来自不同的图表。

目前我的代码只能从一张纸上获取多个数据,这意味着我可以从一张纸上绘制2张图。 我的代码目前是:

Sub ChartSheet()

    Dim ChartSheet1 As Chart
    Set ChartSheet1 = Charts.Add
    With ChartSheet1
        .SetSourceData Source:=Sheets("Sheet1").Range("E12:E6232, Y12:Y6232")
        .ChartType = xlLine
        .HasTitle = True
        .ChartTitle.Characters.Text = "Test Chart"
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "x"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "y"
    End With

End Sub

我想说的是:

.SetSourceData Source:=Sheets("Sheet1").Range("E12:E6232, Y12:Y6232")
.SetSourceData Source:=Sheets("Sheet2").Range("D12:E23")
.SetSourceData Source:=Sheets("Sheet3").Range("Y12:Y6232, G27:G496, H3:5977")

等等.. 但是,当我这样做时,我的代码只打印.SetSoureData

的最后一行

希望你们中的一些人可以帮助我解决这个问题,非常感谢你们提出的建议:)

更新 我通过循环找到了解决方法,但这不是我的总答案 但这是我的其他代码:

Sub MultiSheetPlot()

Dim cht As Chart, s As Series, xRng As Range
Dim i As Long, chartName As String

    Set cht = Charts.Add
    cht.ChartType = xlLine


    For i = 1 To 3

        chartName = "Sheet" & i
        Set xRng = Sheets("Sheet1").Range("A1:A20, C1:C20")

        With cht.SeriesCollection.NewSeries()
            .Values = xRng
            .Name = chartName
        End With

    Next i

End Sub

此代码中的问题是它忽略了我定义的最后一个范围,如C10:C20

2 个答案:

答案 0 :(得分:2)

响应您的最新更新,我有Sub每次从 MultiSheetPlot Sub调用时都会创建一个系列。

您实际上可以向此Sub添加更多参数(只要您记得在调用中传递它们)。

Option Explicit

Dim cht                             As Chart

Sub MultiSheetPlot()

Set cht = Charts.Add
cht.ChartType = xlLine

' call the series creation chart function (each time for each series you want to add to the existing chart
Call Create_SeriesChart("Sheet1", Sheets("Sheet1").Range("E12:E6232"), 1, True, msoThemeColorText1)
Call Create_SeriesChart("Sheet1", Sheets("Sheet1").Range("Y12:Y6232"), 1, True, msoThemeColorText1)

End Sub

' ------ this Sub creates a series to the chart, it receives the following parameters: ------
' 1. seriesName - String
' 2. serValues - Range
' 3. lineWeight - Double (the weight of the line)
' 4. lineVis - Boolean (if you want to hide a certail series)
' 5. lineColor - MsoColorType (using the current's PC Theme colors

Sub Create_SeriesChart(seriesName As String, serValues As Range, lineWeight As Double, lineVis As Boolean, lineColor As MsoColorType)

Dim Ser                             As Series

Set Ser = cht.SeriesCollection.NewSeries

With Ser
    .Name = seriesName
    .Values = serValues

    .Format.Line.Weight = lineWeight
    If lineVis = True Then
        .Format.Line.Visible = msoTrue
    Else
        .Format.Line.Visible = msoFalse
    End If
    .Format.Line.ForeColor.ObjectThemeColor = lineColor   ' Line color Black

End With

End Sub

答案 1 :(得分:0)

我的解决方案是使用plage的名称,您可以给出您的范围名称 就像我的照片一样: enter image description here

每当您想要使用这些数据时,您只需要调用它们 就像这个plage一样,我给他们起了名字Poste_EtatDeLaDemande

enter image description here

当我想要使用这些元素时,我只需将其称为Range("Poste_EtatDeLaDemande") Excel会发现它不需要告诉它们它在哪里; )