VBA将图像复制到图表给出错误

时间:2015-08-25 17:12:18

标签: vba

我正在编写一个代码来填充数据表,然后通过调用其他人制作的模块来获取并保存它的图像。我的部分代码能够创建图像,而被调用的子工作用于为其他表创建图像,但我认为我的部分缺少某些东西。这是循环遍历不同产品线并创建其表格图像的for循环。

For i = 0 To UBound(allLines)
    Cells(bcell, 2) = Cells(product, 2)
    Cells(bcell, 3) = Cells(product, 3)
    Cells(bcell, 4) = Cells(product, 4)
    Cells(bcell, 5) = Cells(product, 5)
    Cells(ecell, 2) = Cells(product, 6)
    Cells(ecell, 3) = Cells(product, 7)
    Cells(ecell, 4) = Cells(product, 8)
    Cells(ecell, 5) = Cells(product, 10)
    Range(Cells(acell, 1), Cells(ecell, 5)).Select
    Selection.Copy
    Range("M14").Select
    ActiveSheet.Pictures.Paste.Select
    myfilename = Year(Now) & " " & MonthName(Month(Now)) & " " & allLines(i)     & " Production Status Metrics" ' & ".jpg"
    EndFilePath = "C:\Users\*******\Documents\**********\TEST FILES\" &     myfilename 'edited for privacy
    Call ExportMyPicture(allLines(i), EndFilePath) 'Module: Export_Cells_to_File
    Range("A1").Select
    acell = acell + 5
    ecell = ecell + 5
    bcell = bcell + 5
    product = product + 1
Next

这是调用到我错误的行的子

 Sub ExportMyPicture(SelectedLine As String, EndFilePath As String)

 Dim MyChart As String, MyPicture As String
 Dim PicWidth As Long, PicHeight As Long

 Application.ScreenUpdating = False

 MyPicture = Selection.Name
 With Selection
       PicHeight = .ShapeRange.Height
       PicWidth = .ShapeRange.Width
 End With

 Charts.Add
 ActiveChart.Location Where:=xlLocationAsObject, Name:=SelectedLine & " Actions" 'This line gives the error

错误消息读取对象变量或未设置块变量

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试制作嵌入式图表。在这种情况下,图表有一个称为图表对象的容器对象,它位于图表和包含图纸之间。您可以将其作为图表对象添加到目标工作表中,而不是创建图表然后调整其位置。类似的东西:

Sub test()
    Dim mySheet As Worksheet
    Set mySheet = Sheets(1)
    Dim PicWidth As Long, PicHeight As Long
    PicWidth = 200
    PicHeight = 100
    Dim CO As ChartObject
    Dim CH As Chart

    Set CO = mySheet.ChartObjects.Add(10, 10, PicWidth, PicHeight)
    Set CH = CO.Chart
    CH.ChartType = xlXYScatter
    CO.Activate
End Sub

Add方法的4个参数是left, top. width, height