2013-02-27 3 views
0

Excel의 통합 문서에서 모든 차트를 그래픽으로 쉽게 내보내는 방법을 찾으려고합니다. 다음 코드를 가지고 있습니다 :모든 차트를 그래픽으로 내보내기

Option Explicit 

Sub ExportChart() 
    ' Export a selected chart as a picture 
    Const sSlash$ = "/" 
    Const sPicType$ = ".png" 
    Dim sChartName$ 
    Dim sPath$ 
    Dim sBook$ 
    Dim objChart As ChartObject 


    On Error Resume Next 
    ' Test if there are even any embedded charts on the activesheet 
    ' If not, let the user know 
    Set objChart = ActiveSheet.ChartObjects(1) 
    If objChart Is Nothing Then 
    MsgBox "No charts have been detected on this sheet", 0 
    Exit Sub 
    End If 


    ' Test if there is a single chart selected 
    If ActiveChart Is Nothing Then 
    MsgBox "You must select a single chart for exporting ", 0 
    Exit Sub 
    End If 


Start: 
    sChartName = Application.InputBox("Please Specify a name for the exported chart" & vbCr & _ 
    "There is no default name available" & vbCr & _ 
    "The chart will be saved in the same folder as this file", "Chart Export", "") 

    ' User presses "OK" without entering a name 
    If sChartName = Empty Then 
    MsgBox "You have not entered a name for this chart", , "Invalid Entry" 
    GoTo Start 
    End If 

    ' Test for Cancel button 
    If sChartName = "False" Then 
    Exit Sub 
    End If 

    ' If a name was given, chart is exported as a picture in the same 
    ' folder location as their current file 
    sBook = ActiveWorkbook.Path 
    sPath = sBook & sSlash & sChartName & sPicType 
    ActiveChart.Export Filename:=sPath, FilterName:="PNG" 

End Sub 

이것은 활성 차트를 내 보내지 만 모든 차트를 내보내려면 어떻게해야합니까? 보너스는 차트의 출처가 워크 시트의 이름을 따서 명명 된 경우 나타납니다.

답변

5
Sub Test() 

Dim sht As Worksheet, cht As ChartObject 
Dim x As Integer 

    For Each sht In ActiveWorkbook.Sheets 
     x = 1 
     For Each cht In sht.ChartObjects 
      cht.Chart.Export "C:\local files\temp\" & sht.Name _ 
           & "_" & x & ".png", "PNG" 
      x = x + 1 
     Next cht 

    Next sht 

End Sub 
+0

파일 이름에서 "_1"의 근거는 무엇입니까? – fromabove

+1

한 장에 두 개 이상의 차트가있는 경우 동일한 파일 이름을 부여 할 수 없습니다 ... 귀하의 질문에서 차트가 몇 개인 지 명확하지 않았습니다. –

+1

이 경우 한 장에 하나의 차트 만 표시되지만 통화는 좋습니다. 당신의 도움을 주셔서 감사합니다! – fromabove

0

빠르고 지저분합니다.
워크 시트와 각 시트의 모든 차트 개체를 반복하려면이 코드를 코드 하단에 넣으십시오.

파일이나 상황을 다시 만들 시간이 없으므로 테스트하지 않았습니다. 희망이 있습니다

For each x in worksheets.count then 
    For Each objChart In ActiveSheet.ChartObjects then 
    sChartName = activesheet.name 
    sBook = ActiveWorkbook.Path 
    sPath = sBook & sSlash & sChartName & sPicType 
    ActiveChart.Export Filename:=sPath, FilterName:="PNG" 
    Next objChart 
Next x 
관련 문제