如何将 Excel 自动转换为 PowerPoint(分步指南)
第 1 步 – 准备您的数据集
- 打开包含相关数据的 Excel 工作表。
- 根据数据创建图表。例如,我们将使用范围 B4:D8 来表示销售信息。
第 2 步 – 访问 Visual Basic 编辑器
- 在 Excel 中,转到功能区上的开发人员选项卡。
- 选择“Visual Basic”打开Visual Basic 编辑器。
- 或者,按键盘上的Alt + F11(笔记本电脑用户为Fn + Alt + F11)。
步骤 3 – 在 Excel 和 PowerPoint 之间建立链接
要将 Excel 与 PowerPoint 连接,请启用必要的引用:
- 点击工具并选择参考。
- 选中 Microsoft PowerPoint 16.0 对象库旁边的复选框(根据您的 PowerPoint 调整版本)。
- 单击确定。
第 4 步 – 插入模块窗口
- 在 Visual Basic 编辑器中,导航至插入选项卡。
- 选择模块打开模块窗口。
第 5 步 – 添加 VBA 代码
在模块窗口中,输入以下VBA代码:
Option Explicit
Sub Excel_to_PP()
Dim PwrPntAp As New PowerPoint.Application
Dim iPPTFile As PowerPoint.Presentation
Dim iSlide As PowerPoint.Slide
Set iPPTFile = PwrPntAp.Presentations.Add
Dim iSht As Worksheet
For Each iSht In ThisWorkbook.Sheets
If iSht.Name <> "Setting" Then
SetiSlide=iPPTFile.Slides.AddSlide(1,iPPTFile.SlideMaster.CustomLayouts(6))
iSlide.MoveTo (iPPTFile.Slides.Count)
With iSlide.Shapes.Title
.TextFrame.TextRange.Text = iSht.Name
.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255)
.Fill.BackColor.RGB = RGB(150, 150, 150)
.TextEffect.Alignment = msoTextEffectAlignmentCentered
.TextEffect.FontName = "Calibri"
.Height = 50
End With
iSht.UsedRange.CopyPicture xlScreen, xlPicture
iSlide.Shapes.Paste
With iSlide.Shapes(2)
.LockAspectRatio = msoCTrue
.Width = iPPTFile.PageSetup.SlideWidth - 30
.Top = 0
If .Height > iPPTFile.PageSetup.SlideHeight Then
.Height = iPPTFile.PageSetup.SlideHeight - 120
End If
.Left = 0
If .Width > iPPTFile.PageSetup.SlideWidth Then
.Width = iPPTFile.PageSetup.SlideWidth - 30
End If
.Left = (iPPTFile.PageSetup.SlideWidth - .Width) / 2
.Top = 100
End With
End If
Next
End Sub
VBA代码说明:
VBA CODE Explanation:
Dim PwrPntAp As New PowerPoint.Application
Dim iPPTFile As PowerPoint.Presentation
Dim iSlide As PowerPoint.Slide
' Here, PwrPntAp, iPPTFile, and iSlide are variables representing PowerPoint Application, Presentation, and Slide, respectively.
Set iPPTFile = PwrPntAp.Presentations.Add
Dim iSht As Worksheet
' We're opening PowerPoint Application and declaring a variable for an Excel worksheet.
If iSht.Name <> "Setting" Then
Set iSlide = iPPTFile.Slides.AddSlide(1, iPPTFile.SlideMaster.CustomLayouts(6))
iSlide.MoveTo (iPPTFile.Slides.Count)
With iSlide.Shapes.Title
.TextFrame.TextRange.Text = iSht.Name
.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255)
.Fill.BackColor.RGB = RGB(150, 150, 150)
.TextEffect.Alignment = msoTextEffectAlignmentCentered
.TextEffect.FontName = "Calibri"
.Height = 50
End With
iSht.UsedRange.CopyPicture xlScreen, xlPicture
End If
' We're copying Excel data and creating a PowerPoint slide where we'll place our data.
iSlide.Shapes.Paste
With iSlide.Shapes(2)
.LockAspectRatio = msoCTrue
.Width = iPPTFile.PageSetup.SlideWidth - 30
.Top = 0
If .Height > iPPTFile.PageSetup.SlideHeight Then
.Height = iPPTFile.PageSetup.SlideHeight - 120
End If
.Left = 0
If .Width > iPPTFile.PageSetup.SlideWidth Then
.Width = iPPTFile.PageSetup.SlideWidth - 30
End If
.Left = (iPPTFile.PageSetup.SlideWidth - .Width) / 2
.Top = 100
End With