将Excel中的特定数据传输到PowerPoint中的单个幻灯片中

时间:2018-09-26 02:35:53

标签: excel vba powerpoint

我必须在一个大型电子表格中获取信息,然后将其传输到PowerPoint中。电子表格中的每一行都是一个人的信息。电子表格中有1000行,这意味着我需要创建1000张幻灯片,每张幻灯片一张。但是,并非所有列都是必需的。这是我的数据示例:

A small similar data set

最后我想要什么(请注意,并非所有列都是必需的):

One slide, note that not all columns are needed

另一张幻灯片:

Another slide

有没有手动复制和粘贴的方法?必须有代码以加快该过程。

1 个答案:

答案 0 :(得分:1)

假定已经创建了仅包含一张幻灯片作为模板的PowerPoint演示文稿。在单张幻灯片中创建必要的文本框。文本框的名称可以从选择窗格中找到。添加并尝试对代码进行必要的修改

Sub Update()
Dim exl As Excel.Application, wb As Excel.Workbook
Dim col1, col2, col3, col4, Strow, EndRow As Integer
Dim Pitcher, Hand, No, BallPark As Variant
col1 = 1  'Column no Corresponding to Pitcher
col2 = 2  'Column no Corresponding to Throwing Hand
col3 = 3  'Column no Corresponding to Number
col4 = 4  'Column no Corresponding to Ball Park
Strow = 2  'Strating row of the Data
EndRow = 10  'Last row of the Data

Set exl = CreateObject("Excel.Application")
exl.Visible = False
Set wb = exl.Workbooks.Open("C:\Users\user\desktop\Stackover.xlsx")   '  full path of excel file containing data


    For X = Strow To EndRow

    Pitcher = wb.sheets(1).Cells(X, col1).Value
    Hand = wb.sheets(1).Cells(X, col2).Value
    No = wb.sheets(1).Cells(X, col3).Value
    BallPark = wb.sheets(1).Cells(X, col4).Value

    Pitcher = IIf(IsError(Pitcher), "", Pitcher)
    Hand = IIf(IsError(Hand), "", Hand)
    No = IIf(IsError(No), "", No)
    BallPark = IIf(IsError(BallPark), "", BallPark)

    ActivePresentation.Slides(1).Copy
    sno = X - Strow + 2
    ActivePresentation.Slides.Paste sno

    '''' Name of textboxes could be found from Selection Pane
    ActivePresentation.Slides(sno).Shapes("TextBox 1").TextFrame.TextRange.Text = Pitcher
    ActivePresentation.Slides(sno).Shapes("TextBox 2").TextFrame.TextRange.Text = Hand
    ActivePresentation.Slides(sno).Shapes("TextBox 3").TextFrame.TextRange.Text = No
    ActivePresentation.Slides(sno).Shapes("TextBox 4").TextFrame.TextRange.Text = BallPark
    Next

wb.Close False
exl.Quit
Set wb = Nothing
Set exl = Nothing

End Sub