Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PowerPoint VBA: Create a Table with a Macro

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can create a table in PowerPoint with VBA by adding a Shape whose HasTable property is set to True, then filling its cells through the Table object. The shortest working pattern is: get a slide, call Shapes.AddTable(rows, columns, left, top, width, height), then write to shape.Table.Cell(row, column).Shape.TextFrame.TextRange.Text. That gives you a real PowerPoint table you can format, not a picture or a group of rectangles.

This is useful when you need to generate the same table layout across many slides or decks, or when a table has to be built from data you already have in Excel or a text file. The steps below assume the desktop version of PowerPoint on Windows or Mac, since the Visual Basic Editor is not available in PowerPoint for the web or on mobile.

What You'll Need

  • PowerPoint desktop app (Microsoft 365, PowerPoint 2019, 2021 or later) on Windows or Mac. The web and mobile versions do not include the VBA editor.
  • A presentation open with at least one slide, or a plan to add one in code.
  • The Developer tab enabled, or another way to open the Visual Basic Editor (Alt+F11 on Windows, or Tools > Macro > Visual Basic Editor on Mac).
  • Basic familiarity with the PowerPoint object model: presentations contain slides, slides contain shapes, and a table is a shape with a table inside it.

Step-by-Step

  1. Open the Visual Basic Editor. In PowerPoint, press Alt+F11 on Windows. On Mac, go to Tools > Macro > Visual Basic Editor. If the Developer tab is not visible on Windows, enable it via File > Options > Customize Ribbon and tick Developer. Menu labels can differ slightly by version and platform.

  2. Insert a module. In the editor, right-click your presentation's VBA project in the Project pane and choose Insert > Module. Code placed in a standard module can be run from the Macro dialog or from the editor with F5.

  3. Write the macro skeleton. Start with a Sub that declares your slide and table shape variables. For example:

    Sub CreateTable()
        Dim sld As Slide
        Dim tblShape As Shape
        Set sld = ActivePresentation.Slides(1)
    End Sub

    Change Slides(1) to the slide you actually want, or add a new slide first with Set sld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank).

  4. Add the table shape. Call AddTable with the number of rows and columns, plus position and size in points (1 inch = 72 points). For example, a 4-row, 3-column table placed one inch from the left and top, 6 inches wide and 3 inches tall:

    Set tblShape = sld.Shapes.AddTable(4, 3, 72, 72, 432, 216)

    The returned shape has HasTable = True and exposes a Table object.

  5. Fill the cells. Cell references are 1-based: row 1, column 1 is the top-left cell. Write text like this:

    Dim r As Long, c As Long
    For r = 1 To 4
        For c = 1 To 3
            tblShape.Table.Cell(r, c).Shape.TextFrame.TextRange.Text = "R" & r & "C" & c
        Next c
    Next r

    In practice you would replace the generated string with your own headers and data, or read values from an array.

  6. Format the table. Common adjustments are column widths, row heights, font size and header styling. For example, to bold the first row and set its font size:

    Dim hdr As Cell
    For Each hdr In tblShape.Table.Rows(1).Cells
        With hdr.Shape.TextFrame.TextRange.Font
            .Bold = msoTrue
            .Size = 14
        End With
    Next hdr

    You can also set tblShape.Table.Columns(1).Width or tblShape.Table.Rows(1).Height in points. The exact property names are stable across recent versions, but check the Object Browser (F2) if a property is rejected.

  7. Run the macro. Press F5 in the editor, or close the editor and use View > Macros on Windows (Developer > Macros) to pick CreateTable and run it. The table appears on the slide you targeted.

  8. Save the file as a macro-enabled presentation. Use .pptm rather than .pptx, otherwise PowerPoint strips the VBA code when you save. If you plan to share it, note that recipients will see a macro security prompt.

Common Mistakes

  • Off-by-one cell references. PowerPoint tables start at row 1, column 1, not 0. A loop from 0 to Rows.Count - 1 will throw an error.
  • Using Shapes.AddShape and expecting a table. Only AddTable (or converting an existing shape) produces a shape with HasTable = True. A rectangle with text in it is not a table.
  • Forgetting the .Shape between Cell and TextFrame. The correct chain is Table.Cell(r, c).Shape.TextFrame.TextRange.Text. Leaving out .Shape is a very common compile error.
  • Saving as .pptx. The code is discarded on save. Use .pptm.
  • Assuming the table fits the slide. AddTable accepts any size, including sizes larger than the slide. Check your left, top, width and height against the slide dimensions (typically 960x540 points for 16:9).
  • Running on a slide that does not exist. Slides(1) fails if the presentation has no slides. Add a slide first or guard with a check.

Tips

  • Store your data in a two-dimensional array and loop through it, rather than hard-coding each cell. That makes it easy to swap the source later.
  • Use With tblShape.Table blocks to shorten repeated references and make the code easier to read.
  • Set the table style after creation by applying a built-in style through the table's style properties, or format borders and fills manually if you need a specific look.
  • If you need the same table on many slides, wrap the steps in a loop over ActivePresentation.Slides and add a fresh table to each one.
  • Keep a backup copy before running a macro that modifies existing slides, since VBA changes are not always easy to undo in bulk.

Troubleshooting

If the macro does nothing, check that the code is in a standard module and not in a slide or form module, and that you are running the correct Sub name. If you get a "Subscript out of range" error, the slide index you referenced does not exist; add a slide or change the index. If you get "Object doesn't support this property or method," you are probably calling a table property on a shape that is not a table; confirm HasTable is True before touching Table. If the table appears but the text is missing, check that you wrote to TextRange.Text and not to a read-only property. If the code runs but the file loses the macro after saving, you saved as .pptx; use Save As and choose the macro-enabled format. If the Developer tab or editor is missing entirely, you are likely in PowerPoint for the web or on mobile, where VBA is not supported; open the file in the desktop app instead.

Frequently Asked Questions

Can I create a table with VBA in PowerPoint for the web?

No. VBA is only available in the desktop versions of PowerPoint. The web and mobile versions do not include the Visual Basic Editor or support running macros.

How do I add rows or columns after the table is created?

Use the table's Rows.Add and Columns.Add methods, specifying the position. Existing cell content shifts accordingly, so plan the order of your edits.

Why does my macro fail with "Object required"?

Usually a variable was declared but never set, or you referenced a shape that is not a table. Set the shape variable with Set and confirm HasTable before using .Table.

Can I read table data from Excel and build the PowerPoint table from it?

Yes, in principle, by opening the workbook from VBA and looping through its cells. That requires the Excel object library to be referenced, and the workbook must be available at run time. Keep the data small and close the workbook when done.

Does the table keep its formatting if I edit the slide later?

Yes, a VBA-created table is a normal PowerPoint table. You can edit it by hand afterwards, and the formatting you applied in code remains until you change it.

What is the difference between AddTable and AddShape?

AddTable creates a shape that contains a real table grid with rows and columns. AddShape creates a drawing shape such as a rectangle or oval, which has no table structure.