PowerPoint Macro: Adding a Row to Tables on an Active Slide

This PowerPoint VBA macro adds a row to tables on an active slide.

Macro Example

Sub AddRowToTables()

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasTable Then .Table.Rows.Add
    End With
  Next shp

End Sub

Description

Variable Declaration

Sub...

  Dim shp As Shape

The 3rd line declares an object variable named shp that refers to a Shape object.

The Shape object represents a placeholder, shape, freeform, OLE object or picture.

obj-PowerPoint-Shape

Starts Looping Through All Shapes

Sub...

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes

The 5th line starts to loop through all the elements (Shape) of a collection (Shapes).

(Application.)ActiveWindow

  For Each shp In ActiveWindow...

The Application.ActiveWindow property returns the DocumentWindow object that represents an active document window.

obj-PowerPoint-Application-ActiveWindow

The ActiveWindow property is a member of the PowerPoint.Global class, so we can omit the Application property.

obj-PowerPoint-Global-ActiveWindow

ActiveWindow.Selection

  For Each shp In ActiveWindow.Selection...

The ActiveWindow.Selection (DocumentWindow.Selection property) returns the Selection object that represents the selection in the document window.

obj-PowerPoint-DocumentWindow-Selection

Selection.SlideRange

  For Each shp In ActiveWindow.Selection.SlideRange...

The Selection.SlideRange property returns the SlideRange object that represents a range of selected slides.

obj-PowerPoint-Selection-SlideRange

In this macro it represents an active slide.

SlideRange.Shapes

  For Each shp In ActiveWindow.Selection.SlideRange.Shapes

The SlideRange.Shapes property returns a Shapes collection object that represents all the shapes that have been placed on a slide.

obj-PowerPoint-SlideRange-Shapes

Starts the With structure

Sub SelectTable()

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp

The 6th line starts the With…End With statement.
This statement tells PowerPoint that any action apply to the shp (Shape object).

Checks If the Shape Has a Table and…

Sub...

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasTable Then .Table.Rows.Add

The 7th line checks if the shape is a table, and if so adds a row.

Shape.HasTable

    With shp
      If .HasTable...

The shp.HasTable (Shape.HasTable property) returns whether the shape is a table.

Shape.Table

    With shp
      If .HasTable Then .Table...

The Shape.Table property returns a Table object that represents a table in a shape.

obj-PowerPoint-Shape-Table

Table.Rows

    With shp
      If .HasTable Then .Table.Rows...

The Table.Rows property returns a Rows collection object that represents all the rows in a table.

obj-PowerPoint-Table-Rows

Rows.Add

    With shp
      If .HasTable Then .Table.Rows.Add

The Rows.Add method adds a row to the table.

obj-PowerPoint-Rows-Add

Loops Back

Sub...

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasTable Then .Table.Rows.Add
    End With
  Next shp

The 9th line loops back to evaluate the next shape.

After every shape is evaluated, this macro ends.

Sub AddRowToTables()

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasTable Then .Table.Rows.Add
    End With
  Next shp

End Sub

Properties

http://www.relief.jp/itnote/archives/powerpoint-vba-add-row-table.php

Apply to

  • PowerPoint 2013
  • PowerPoint 2010
  • PowerPoint 2007