【发布时间】:2019-08-17 04:04:38
【问题描述】:
我正在尝试遍历 Excel 中的 3 行并将它们复制并粘贴到三个单独的幻灯片中。
此代码将复制所有 3 行并将所有 3 行粘贴到三个单独的幻灯片中。但是,我正在尝试复制幻灯片 1 中的第 1 行、幻灯片 2 中的第 2 行和幻灯片 3 中的第 3 行。有什么办法吗?
Sub Copy_Paste_ExcelPPT()
Dim PPTApp As Powerpoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim rng As Range
Dim row As Range
Dim cell As Range
Dim rngarray As Variant
Dim ExcRng As Range
'Create new instance of PowerPoint
Set PPTApp=New PowerPoint.Application
PPTApp. Visible=True
'Create a new presentation
Set PPTPres=PPTApp.Presentations.Add
'Loop through each row in the excel file
Set rng =Range("F4:H6")
For Each row In rng.Rows
For Each row In row.Cells
'Create an array that houses references to the ranges we want to export
rngarray= Array(rng)
'Loop through this array, copy the row, create a new slide and paste the row in a different slide
For x=LBound(rngarray) To UBound(rngarray)
Set a reference to the range we want to export
Set ExcRng=rngarray(x)
'Copy the range
ExcRng.Copy
'Create a new slide in the presentation
Set PPTSlide=PPTPres.Slides.Add(x+1,ppLayoutBlank)
'Paste the range in the slide
PPTSlide.Shapes.Paste
Next x
Next cell
Next row
End Sub
此代码将复制所有 3 行并将所有 3 行粘贴到三个单独的幻灯片中。我正在尝试复制幻灯片 1 中的第 1 行、幻灯片 2 中的第 2 行和幻灯片 3 中的第 3 行。有什么办法吗?
【问题讨论】:
-
复制每个范围,例如'Range("F4:F6").Copy'等并粘贴到新幻灯片中?
-
@GMalc 感谢您的回复!我正在研究将 F4:H4、F5:H5 和 F6:H6 的每个“行”复制到三个单独的幻灯片中。 F4:H4 粘贴在幻灯片 1 中,F5:H5 粘贴在幻灯片 2 中,F6:H6 粘贴在幻灯片 3 中。我正在考虑使用循环来执行此操作,这样当我有 50 多行时,我不必费力如您的答案所示对每一行进行编码。
标签: excel vba copy-paste powerpoint