Supercharge your PowerPoint productivity with
Supercharge your PPT Productivity with PPTools - Click here to learn more.

Proud member of

PPTools

Image Export converts PowerPoint slides to high-quality images.

PPT2HTML exports HTML even from PowerPoint 2010 and 2013, gives you full control of PowerPoint HTML output, helps meet Section 508 accessibility requirements

Merge Excel data into PowerPoint presentations to create certificates, awards presentations, personalized presentations and more

Resize your presentations quickly and without distortion

Language Selector switches the text in your presentation from one language to another

FixLinks prevents broken links when you distribute PowerPoint presentations

Shape Styles brings styles to PowerPoint. Apply complex formatting with a single click.

Create a grid of rectangles

Problem

Sometimes it's very handy to be able to create an accurately laid out grid of rectangles, for example when you want to lay out a grid so that you can do a scale drawing.

Creating a grid by hand can be tedious, boring and above all, slow. A bit of VBA makes it quick, simple and accurate.

Solution

Draw a rectangle or square covering the area you want to fill with a grid. Run this macro and it'll ask how many columns and rows of rectangles you want to fill the area with.

Sub GridInRectangle()

    Dim oSh As Shape
    Dim oSld As Slide
    Dim sngwidth As Single  ' width/height of a grid rect
    Dim sngheight As Single
    Dim lCols As Long
    Dim lRows As Long
    Dim x As Long   ' which col across are we making
    Dim y As Long   ' which row down are we making
    Dim sngLeft As Single   ' where to draw current rectangle
    Dim sngTop As Single
    Dim sTemp As String

    If Not ActiveWindow.Selection.Type = ppSelectionShapes Then
        MsgBox "Select something, then try again"
        Exit Sub
    End If

    ' get rows/cols from user
    sTemp = InputBox("How many columns?", "Columns")
        If CLng(sTemp) > 0 Then
            lCols = CLng(sTemp)
            sTemp = InputBox("How many rows?", "Rows")
            If CLng(sTemp) > 0 Then
                lRows = CLng(sTemp)
            Else
                Exit Sub
            End If
         Else
            Exit Sub
         End If

    Set oSh = ActiveWindow.Selection.ShapeRange(1)
    Set oSld = oSh.Parent

    sngwidth = oSh.width / lCols
    sngheight = oSh.height / lRows

    For x = 0 To lCols - 1
        For y = 0 To lRows - 1
          ' with osld.Shapes.AddShape(msoShapeRectangle, left, top, width, height)
            With oSld.Shapes.AddShape(msoShapeRectangle, oSh.Left + x * sngwidth, oSh.Top + y * sngheight, sngwidth, sngheight)
                Call .Tags.Add("Grid", "YES")
            End With
        Next
    Next

End Sub

See How do I use VBA code in PowerPoint? to learn how to use this example code.


Did this solve your problem? If so, please consider supporting the PPT FAQ with a small PayPal donation.
Page copy protected against web site content infringement by Copyscape Contents © 1995 - 2022 Stephen Rindsberg, Rindsberg Photography, Inc. and members of the MS PowerPoint MVP team. You may link to this page but any form of unauthorized reproduction of this page's contents is expressly forbidden.

Supercharge your PPT Productivity with PPTools

content authoring & site maintenance by
Friday, the automatic faq maker (logo)
Friday - The Automatic FAQ Maker

Create a grid of rectangles
http://www.pptfaq.com/FAQ00958_Create_a_grid_of_rectangles.htm
Last update 07 June, 2011
Created: