2012-08-13 3 views
8

"Graphs"시트의 차트를 jpeg 파일로 내보내는 버튼을 만들려고합니다. 이것은 내가 가지고있는 코드입니다, 그러나 그것은이 오류를 보여주는 유지 :이 특별히차트를 이미지로 내보내기 - 버튼 클릭 포함

runtime error 424: object required

:

Set myChart = Graphs.ChartObjects(3).Name = "Chart4" 

을 그리고 여기에 코드

Sub ExportChart() 
    Dim myChart As Chart 
    Dim myFileName As String 
    Set myChart = Graphs.ChartObjects(3).Name = "Chart4" 
    myFileName = "myChart.jpg" 
    On Error Resume Next 
    Kill ThisWorkbook.Path & "\" & myFileName 
    myChart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG" 
    MsgBox "OK" 
    Set myChart = Nothing 
End Sub 

모두에게 감사입니다!

답변

18

이게 너가하려는거야?

또한 jpg로 저장하려는 경우 왜 png 필터가 필요합니까? "myChart.jpg"를 "myChart.png"로 변경했습니다. 해당되는 경우 변경하십시오.

Sub ExportChart() 
    Dim objChrt As ChartObject 
    Dim myChart As Chart 

    Set objChrt = Sheets("Graphs").ChartObjects(3) 
    Set myChart = objChrt.Chart 

    myFileName = "myChart.png" 

    On Error Resume Next 
    Kill ThisWorkbook.Path & "\" & myFileName 
    On Error GoTo 0 

    myChart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG" 

    MsgBox "OK" 
End Sub 
+0

너무 빠르다! OP에 대한 한 메모는 Siddharth의 답변에 포함되어 있습니다. 가능한 한 빨리 On Error GoTo를 코드에 추가하면 오류를 숨길 수 있습니다. 이 경우 ChartObject의 차트 개체가 필요합니다 :) 이것은 항상 혼란 스럽습니다. –

+0

나는 지난 며칠 동안 활동하지 않고 보충하려고 노력 중이다. D –

+0

와우는 훌륭합니다. 감사! – pufAmuf

1

감사합니다. 이미지의 모든 차트를 추출해야하며 거의 자정입니다. 위의 코드를 약간 변경하면 트릭을 만들 수 있습니다.

Sub ExportChart() 
    Dim WS As Excel.Worksheet 
    Dim SaveToDirectory As String 

    Dim objChrt As ChartObject 
    Dim myChart As Chart 

    SaveToDirectory = ActiveWorkbook.Path & "\" 

    For Each WS In ActiveWorkbook.Worksheets 
     WS.Activate 'go there 
     For Each objChrt In WS.ChartObjects 
      objChrt.Activate 
      Set myChart = objChrt.Chart 

      myFileName = SaveToDirectory & WS.Name & "_" & objChrt.Index & ".png" 

      On Error Resume Next 
      Kill SaveToDirectory & WS.Name & Index & ".png" 
      On Error GoTo 0 

      myChart.Export Filename:=myFileName, Filtername:="PNG" 
     Next 
    Next 

    MsgBox "OK" 
End Sub 
관련 문제