使用另一个工作表中的导入数据构建图表会产生错误1004

时间:2015-12-09 12:57:23

标签: excel vba excel-vba

我有这个宏来构建一个图表,当在一张工作表中使用表格填充从不同工作表导入的数据时,该图表会出现错误1004。当我使用完全相同的表但在当前工作表中手动创建时,它工作正常。这是我的代码。我可以帮忙吗?

Sub MacroChart()
    'Copy data from "Sheet1" to Sheet2
    'Copying column by column because I read somewhere that it may solve the problem.
    'Unfortunatly, not.
    n = 3
    While Worksheets("Sheet1").Cells(n, 1) <> ""
        n = n + 1
    Wend
    n = n - 1
    'I need to skip column B
    from1 = "A3:A" & n
    from2 = "C3:C" & n
    from3 = "D3:D" & n
    from4 = "E3:E" & n
    from5 = "F3:F" & n
    n = n - 2
    to1 = "A1:A" & n
    to2 = "B1:B" & n
    to3 = "C1:C" & n
    to4 = "D1:D" & n
    to5 = "E1:E" & n
    'The current table will have 5 columns and 54 lines, all cells filled
    With Worksheets("Sheet2")
        .Range(to1).Value = Worksheets("Sheet1").Range(from1).Value
        .Range(to2).Value = Worksheets("Sheet1").Range(from2).Value
        .Range(to3).Value = Worksheets("Sheet1").Range(from3).Value
        .Range(to4).Value = Worksheets("Sheet1").Range(from4).Value
        .Range(to5).Value = Worksheets("Sheet1").Range(from5).Value
    End With
    area = "$A$1:$E$" & n

    'Creating the chart
    'The code stops on line "FullSeriesCollection(2).ChartType..."
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Worksheets("Sheet2").Range(area)
    ActiveChart.FullSeriesCollection(1).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(1).AxisGroup = 1
    ActiveChart.FullSeriesCollection(2).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(2).AxisGroup = 1
    ActiveChart.FullSeriesCollection(3).ChartType = xlLine
    ActiveChart.FullSeriesCollection(3).AxisGroup = 1
End Sub

1 个答案:

答案 0 :(得分:0)

您需要在.Item()之后使用.FullSeriesCollection并且有一种更简单的方法来计算最后一行:

Sub MacroChart()
Dim n As Long, _
    wS1 As Worksheet, _
    ws2 As Worksheet, _
    Area As String, _
    cH As Chart, _
    Sh as Shape
    'Copy data from "Sheet1" to Sheet2
    'Copying column by column because I read somewhere that it may solve the problem.
    'Unfortunatly, not.
    Set wS1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    n = wS1.Range("A" & wS1.Rows.Count).End(xlUp).Row

    'I need to skip column B
    from1 = "A3:A" & n
    from2 = "C3:C" & n
    from3 = "D3:D" & n
    from4 = "E3:E" & n
    from5 = "F3:F" & n

    n = n - 2
    to1 = "A1:A" & n
    to2 = "B1:B" & n
    to3 = "C1:C" & n
    to4 = "D1:D" & n
    to5 = "E1:E" & n
    'The current table will have 5 columns and 54 lines, all cells filled
    With ws2
        .Range(to1).Value = wS1.Range(from1).Value
        .Range(to2).Value = wS1.Range(from2).Value
        .Range(to3).Value = wS1.Range(from3).Value
        .Range(to4).Value = wS1.Range(from4).Value
        .Range(to5).Value = wS1.Range(from5).Value
    End With
    Area = "$A$1:$E$" & n

    'Creating the chart
    'The code stops on line "FullSeriesCollection(2).ChartType..."
    Set Sh = ws2.Shapes.AddChart2(201, xlColumnClustered)
    Set cH = Sh.Chart
    With cH
        .SetSourceData Source:=w2.Range(Area)
        .FullSeriesCollection.Item(1).ChartType = xlColumnClustered
        .FullSeriesCollection.Item(1).AxisGroup = 1
        .FullSeriesCollection.Item(2).ChartType = xlColumnClustered
        .FullSeriesCollection.Item(2).AxisGroup = 1
        .FullSeriesCollection.Item(3).ChartType = xlLine
        .FullSeriesCollection.Item(3).AxisGroup = 1
    End With
End Sub