VBA图表对象 - 1004错误

时间:2014-09-25 17:38:32

标签: excel-vba vba excel

我写了一个宏来复制某个范围的图片并将其复制到图表对象中。代码工作正常,但是当我增加范围的大小时,我得到运行时间1004错误'应用程序定义的或对象定义的错误'。即......我将U40改为U50。

有没有人看到我遗漏的问题,可能会导致此错误?

Sub RangePicSales()
With Worksheets("Dashboard").Range("F8:U40")
    .CopyPicture xlScreen, xlBitmap
    With ActiveSheet.ChartObjects.Add(.Left, .Top + .Height + 1, .Width, .Height).Chart
        .Paste
        With .Shapes(1)
            .Placement = xlMove
            .Left = -4
            .Top = -4
        End With
        .Export Filename:="RemovedFilePathForPost\WeeklySalesDashboard.png", Filtername:="PNG"
        .Parent.Delete
    End With
End With

End Sub

1 个答案:

答案 0 :(得分:0)

无法解释原因,但这样做效果更好 - 与调整图表大小相比,而不是尝试以合适的尺寸创建图表...

Sub Tester()
Dim rng, co As Object, cht As Chart

    Set rng = ActiveSheet.Range("A1:M1000")
    rng.CopyPicture appearance:=xlScreen, Format:=xlBitmap

    Set co = ActiveSheet.ChartObjects.Add(1, 1, 100, 100)
    With co
        .Width = rng.Width
        .Height = rng.Height
        .Chart.Paste
        .Chart.Export Filename:="C:\_Stuff\tester2.png", Filtername:="PNG"
        .Delete
    End With

End Sub

我能够导出A1:M1000

的图片