简体   繁体   English

使用 VBA 将范围从 Excel 工作表复制到 PowerPoint 中的表格

[英]Copy range from Excel sheet to table in PowerPoint using VBA

I tried this code for copying a range from an Excel sheet to a table in PowerPoint but it was not run.我尝试使用此代码将范围从 Excel 工作表复制到 PowerPoint 中的表格,但未运行。

ppapp.Visible = True 
For Each sh In ThisWorkbook.Sheets 
If sh.Name Like "E_KRI" Then ppapp.ActivePresentation.Slides.Add   
     ppapp.ActivePresentation.Slides.Count + 1, ppLayoutBlank 
     ppapp.ActiveWindow.View.GotoSlide 
     ppapp.ActivePresentation.Slides.Count Set ppSlide = 
     ppapp.ActivePresentation.Slides(ppapp.ActivePresentation.Slides.Count) 
     ppSlide.Select 
     iLastRowReport = Range("B" & Rows.Count).End(xlUp).Row
     Range("A3:J" & iLastRowReport).Copy
     Set tbl = ppapp.ActiveWindow.Selection.ShapeRange.Table
     tbl.Cell(5,3).Shape.paste

I have the following code:我有以下代码:

  1. It sets the PowerPoint app, Pres and slide selections settings.它设置 PowerPoint 应用程序、Pres 和幻灯片选择设置。
  2. Loops through all shapes in the selected slide, and looks for the shape of Table type.循环遍历所选幻灯片中的所有形状,并查找表格类型的形状。
  3. Copies the Range from Excel Worksheet.从 Excel 工作表复制范围。
  4. It selects whatever Table Cell you want as the first row and column to copy the Excel range.它选择您想要的任何表格单元格作为复制 Excel 范围的第一行和第一列。
  5. Paste into exisiting table using either the PowerPoint Table format, or the Excel Range format.使用 PowerPoint 表格格式或 Excel 范围格式粘贴到现有表格中。

     Public Sub ExcelRange_to_PPT_Table() Dim ppApp As PowerPoint.Application Dim ppPres As PowerPoint.Presentation Dim ppTbl As PowerPoint.Shape On Error Resume Next Set ppApp = GetObject(, "PowerPoint.Application") On Error GoTo 0 If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application Set ppPres = ppApp.Presentations.Item(1) Else Set ppPres = ppApp.Presentations.Item(1) End If ppApp.ActivePresentation.Slides(1).Select ppPres.Windows(1).Activate ' find on Slide Number 1 which object ID is of Table type (you can change to whatever slide number you have your table) With ppApp.ActivePresentation.Slides(1).Shapes For i = 1 To .count If .Item(i).HasTable Then ShapeNum = i End If Next End With ' assign Slide Table object Set ppTbl = ppApp.ActivePresentation.Slides(1).Shapes(ShapeNum) ' copy range from Excel sheet iLastRowReport = Range("B" & Rows.count).End(xlUp).row Range("A3:J" & iLastRowReport).Copy ' select the Table cell you want to copy to >> modify according to the cell you want to use as the first Cell ppTbl.Table.Cell(3, 1).Shape.Select ' paste into existing PowerPoint table - use this line if you want to use the PowerPoint table format ppApp.CommandBars.ExecuteMso ("PasteExcelTableDestinationTableStyle") ' paste into existing PowerPoint table - use this line if you want to use the Excel Range format ' ppApp.CommandBars.ExecuteMso ("PasteExcelChartSourceFormatting") End Sub

I have below code to copy excel range to PPT table.我有以下代码可以将 excel 范围复制到 PPT 表格。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM