提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何將單個或所有圖表從Excel工作表導出到PowerPoint?

有時,出於某種目的,您可能需要將圖表或所有圖表從Excel導出到PowerPoint。 本文正在討論如何實現它。

將單個圖表或所有圖表從Excel工作表導出到帶有VBA代碼的PowerPoint中


將單個圖表或所有圖表從Excel工作表導出到帶有VBA代碼的PowerPoint中


本節將介紹VBA代碼,將單個圖表或所有圖表從工作簿導出到PowerPoint。 請做如下。

1。 按 其他 + F11 鑰匙一起打開 Microsoft Visual Basic for Applications 窗口。

2。 在裡面 Microsoft Visual Basic for Applications 窗口中,單擊 工具 > 參考 如下圖所示。

3。 在裡面 參考 - VBAProject 對話框,向下滾動查找並檢查 Microsoft PowerPoint對像庫 選項,然後單擊 OK 按鈕。 看截圖:

4。 然後點擊 插入 > 模塊.

5。 如果要將單個圖表導出到PowerPoint,請轉到在工作表中選擇圖表,然後返回到 Microsoft Visual Basic for Applications 窗口中,將下面的VBA代碼複製並粘貼到模塊窗口中。

VBA代碼:將Excel工作表中的單個圖表導出到PowerPoint

Sub SingleActiveChartToPowerPoint_EarlyBinding1()
'Updated by Extendoffice 2017/9/15
  Dim pptApp As PowerPoint.Application
  Dim pptPres As PowerPoint.Presentation
  Dim pptSlide As PowerPoint.Slide
  Dim pptShape As PowerPoint.Shape
  Dim pptShpRng As PowerPoint.ShapeRange
  Dim xActiveSlideNow As Long
  On Error Resume Next
  If ActiveChart Is Nothing Then
    MsgBox "Select a chart and try again!", vbExclamation, "KuTools For Excel"
    Exit Sub
  End If
  Set pptApp = GetObject(, "PowerPoint.Application")
  If pptApp Is Nothing Then
    Set pptApp = CreateObject("PowerPoint.Application")
    Set pptPres = pptApp.Presentations.Add
    Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
  Else
    If pptApp.Presentations.Count > 0 Then
      Set pptPres = pptApp.ActivePresentation
      If pptPres.Slides.Count > 0 Then
        xActiveSlideNow = pptApp.ActiveWindow.View.Slide.SlideIndex
        Set pptSlide = pptPres.Slides(xActiveSlideNow)
      Else
        Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
      End If
    Else
      Set pptPres = pptApp.Presentations.Add
      Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
    End If
  End If
  ActiveChart.ChartArea.Copy
  With pptSlide
    .Shapes.Paste
    Set pptShape = .Shapes(.Shapes.Count)
    Set pptShpRng = .Shapes.Range(pptShape.Name)
  End With
  With pptShpRng
    .Align msoAlignCenters, True
    .Align msoAlignMiddles, True
  End With
  pptShpRng.Select
End Sub

如果要從工作簿中導出所有圖表,請將以下VBA代碼複製並粘貼到“模塊”窗口中。

VBA代碼:將所有圖表從Excel工作表導出到PowerPoint

Option Explicit
'Updated by Extendoffice 2017/9/15
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim pptSlideCount As Integer
Sub ChartsToPowerPoint()
    Dim xSheet As Worksheet
    Dim xChartsCount As Integer
    Dim xChart As Object
    Dim xActiveSlideNow As Integer
    On Error Resume Next
    For Each xSheet In ActiveWorkbook.Worksheets
        xChartsCount = xChartsCount + xSheet.ChartObjects.Count
    Next xSheet
    If xChartsCount = 0 Then
        MsgBox "Sorry, there are no charts to export!", vbCritical, "Ops"
        Exit Sub
    End If
    Set pptApp = GetObject(, "PowerPoint.Application")
    If pptApp Is Nothing Then
      Set pptApp = CreateObject("PowerPoint.Application")
      Set pptPres = pptApp.Presentations.Add
      Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
    Else
        If pptApp.Presentations.Count > 0 Then
          Set pptPres = pptApp.ActivePresentation
          If pptPres.Slides.Count > 0 Then
            xActiveSlideNow = pptApp.ActiveWindow.View.Slide.SlideIndex
            Set pptSlide = pptPres.Slides(xActiveSlideNow)
          Else
            Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
          End If
        Else
          Set pptPres = pptApp.Presentations.Add
          Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
        End If
    End If
    For Each xSheet In ActiveWorkbook.Worksheets
        For Each xChart In xSheet.ChartObjects
            Call pptFormat(xChart.Chart)
        Next xChart
    Next xSheet
    For Each xChart In ActiveWorkbook.Charts
        Call pptFormat(xChart)
    Next xChart
    
    Set pptSlide = Nothing
    Set pptPres = Nothing
    Set pptApp = Nothing
    MsgBox "The charts were copied successfully to the new presentation!", vbInformation, "KuTools For Excel"
End Sub
Private Sub pptFormat(xChart As Chart)
    Dim xCharTiTle As String
    Dim I As Integer
    On Error Resume Next
    xCharTiTle = xChart.ChartTitle.Text
    xChart.ChartArea.Copy
    pptSlideCount = pptPres.Slides.Count
    Set pptSlide = pptPres.Slides.Add(pptSlideCount + 1, ppLayoutBlank)
    pptSlide.Select
    pptSlide.Shapes.PasteSpecial ppPasteJPG
    If xCharTiTle <> "" Then
        pptSlide.Shapes.AddTextbox msoTextOrientationHorizontal, 12.5, 20, 694.75, 55.25
    End If
    For I = 1 To pptSlide.Shapes.Count
        With pptSlide.Shapes(I)
            Select Case .Type
                Case msoPicture:
                    .Top = 87.84976
                    .left = 33.98417
                    .Height = 422.7964
                    .Width = 646.5262
                Case msoTextBox:
                    With .TextFrame.TextRange
                        .ParagraphFormat.Alignment = ppAlignCenter
                        .Text = xCharTiTle
                        .Font.Name = "Tahoma (Headings)"
                        .Font.Size = 28
                        .Font.Bold = msoTrue
                    End With
                End Select
        End With
    Next I
End Sub

6。 按 F5 鍵或單擊運行按鈕以運行代碼。 然後將打開一個新的PowerPoint,並將所選圖表或所有導入的圖表打開。 你會得到一個 Kutools for Excel 對話框如下圖所示,請點擊 OK 按鈕。


相關文章:



推薦的Excel生產力工具

Kutools for Excel幫助您提前完成工作,並從人群中脫穎而出

  • 超過300強大的高級功能,專為1500工作場景設計,通過70%提高生產力,讓您有更多時間照顧家庭和享受生活。
  • 不再需要記憶公式和VBA代碼,從現在起讓你的大腦休息一下。
  • 成為3分鐘的Excel專家,複雜和重複的操作可以在幾秒鐘內完成,
  • 每天減少成千上萬的鍵盤和鼠標操作,現在告別職業病。
  • 110,000高效人才和300 +世界知名公司的選擇。
  • 60-day full功能免費試用。 60天退款保證。 2多年的免費升級和支持。

將選項卡式瀏覽和編輯帶到Microsoft Office,遠比瀏覽器的選項卡強大

  • Office選項卡專為Word,Excel,PowerPoint和其他Office應用程序設計:Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Andrea89 · 23 days ago
    hello,
    How can I have the some things in PDF ?

    thanks !
  • To post as a guest, your comment is unpublished.
    Antoni · 2 months ago
    Hi, Thank you for code.

    I tried update your code with a little changes but I don't know how can I do it, I need update/change somes Shape on a specific Slide, but I don't know how can i do it.?
    • To post as a guest, your comment is unpublished.
      crystal · 23 days ago
      Hi Antoni,
      Sorry can't help you with that yet. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Ross · 10 months ago
    Thank you, your code worked perfectly for what I needed to do. I will bookmark your page and come back when I need more.