Search code examples
excelvbainsertpowerpoint

How to fill Powerpoint table from Excel data


I have an excel data that contains range from A40 to G1330, but the actual range is from A40 to G63 because the remaining range has formula but the cell is empty "I applied the formula IFERROR make the cell empty"

also I have an exsist powerpoint file and slide number 3 contains of a table with name "Table 1"

what I want is to fill the table which is in the powerpoint file from the excel range from range D40 to E63

I have tried the following code

Dim Name_Attendees_lastVal As Range
Dim sht As Worksheet 
Dim pptapp As PowerPoint.Application
Dim presentation As PowerPoint.presentation
Dim ppslide As PowerPoint.Slide
Dim slidetitle As String
Dim pptfile As String
Dim slideCtr As Integer
Set sht = Sheets("Reports")
Set Name_Attendees_lastVal = sht.Columns(5).Find("*", sht.Cells(1, 2), xlValues, xlPart, xlByColumns,xlPrevious)
sht.Range("D40", Name_Attendees_lastVal).Resize(, 2).Select
pptfile = "C:\Users\habinalshaikh\Desktop\Training\presentation maker\Course Report.pptx" 
Set pptapp = CreateObject("PowerPoint.Application")
pptapp.Visible = True 
pptapp.Presentations.Open (pptfile)
 for i = 40 to Set Name_Attendees_lastVal = sht.Columns(5).Find("*", sht.Cells(1, 2), xlValues, xlPart, xlByColumns, xlPrevious).count  ' this code is to count the number of rows, but it doesn't work
 pptapp.ActivePresentation.Slides(3).Shapes("Table 1").TextFrame.TextRange.Characters.Text = ThisWorkbook.Sheets("Reports").Range("D"&i)   ' this code is to fill the table which is in the PowerPoint file with the names in the excel sheet
next
 for y = 40 to Set Name_Attendees_lastVal = sht.Columns(5).Find("*", sht.Cells(1, 2), xlValues, xlPart, xlByColumns, xlPrevious).count  ' this code is to count the number of rows, but it doesn't work
  pptapp.ActivePresentation.Slides(3).Shapes("Table 1").TextFrame.TextRange.Characters.Text = ThisWorkbook.Sheets("Reports").Range("E"&i)' this code is to fill the table which is in the PowerPoint file with the Attending status in the excel sheet
next

Solution

  • There are some syntax error in below code.

    ' this code is to count the number of rows, but it doesn't work
     for i = 40 to Set Name_Attendees_lastVal = sht.Columns(5).Find("*", sht.Cells(1, 2), xlValues, xlPart, xlByColumns, xlPrevious).count  
    
    • Set can't be used in For clause
    • The Find method returns either Nothing or a Range object; if it returns a Range object, the Row property is used to obtain the row number.
    • The second argument (After) of Find must be within the searched range (Columns(5)); otherwise, a runtime error 13 will occur.

    Microsoft documentation:

    Range.Find method (Excel)


    Option Explicit
    Sub Excel2PPTTable()
        Dim Name_Attendees_lastVal As Range
        Dim oSht As Worksheet
        Dim pptApp As PowerPoint.Application
        Dim pptPres As PowerPoint.presentation
        Dim pptSlide As PowerPoint.Slide
        Dim pptTab As PowerPoint.Table
        Dim pptfile As String
        Dim iR As Long, i As Long
        Const START_ROW = 2 ' change to 1 if there isn't a header row in Table1
        Set oSht = Sheets("Reports")
        pptfile = "C:\Users\habinalshaikh\Desktop\Training\presentation maker\Course Report.pptx"
        Set pptApp = CreateObject("PowerPoint.Application")
        pptApp.Visible = True
        Set pptPres = pptApp.Presentations.Open(pptfile)
        Set pptTab = pptPres.Slides(3).Shapes("Table 1").Table
        If pptTab Is Nothing Then
            MsgBox "Can't find table on the third slide."
        Else
            With oSht.Columns(4)
                Set Name_Attendees_lastVal = .Find("*", .Cells(1), xlValues, xlPart, xlByRows, xlPrevious)
            End With
            iR = START_ROW - 1
            For i = 40 To Name_Attendees_lastVal.Row
                iR = iR + 1
                pptTab.Cell(iR, 1).Shape.TextFrame2.TextRange.Text = oSht.Range("D" & i) ' the names in the excel sheet
                pptTab.Cell(iR, 2).Shape.TextFrame2.TextRange.Text = oSht.Range("E" & i) ' the Attending status in the excel sheet
            Next
        End If
        '    pptPres.Save ' save file
        '    pptPres.Close ' close file
        '    pptApp.Quit    ' close ppt app.
    End Sub
    

    enter image description here