Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Filling Table Cells with a Macro.

Filling Table Cells with a Macro

Written by Allen Wyatt (last updated October 24, 2020)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


As you are working with tables in Word, you may want to fill the various cells in a table with a set value. For instance, you might want to copy something to the Clipboard, and then paste the contents of the Clipboard to each cell in a table. The following macro will do the trick:

Sub PasteToCells()
    Dim TargetRange As Range
    Dim oTargCell As Cell

    If Selection.Cells.Count = 0 Then
        'Quit if no cells in selection
        MsgBox "No cells selected", vbCritical
        Exit Sub
    End If
    On Error Resume Next
    Set TargetRange = Selection.Range
    For Each oTargCell In Selection.Cells
        oTargCell.Range.Paste
    Next oTargCell
    TargetRange.Select
End Sub

The macro starts by checking to make sure that the selection includes some cells. If not, then the user is informed, and the macro is ended. Then the selection is stored in a variable so that it can be selected (again) at the end of the macro. Without this code, the macro would leave the insertion point collapsed in the first cell of the original selection.

The real meat of the macro is in the For ... Next loop. It steps through the cells in the selection and replaces whatever is there with the contents of the Clipboard. Finally, the original selection is again selected, and the macro ends.

You probably noticed that there is an On Error statement in the macro, as well. This statement basically tells Word to ignore any errors and continue with the next statement. Errors that could be triggered include running the macro with nothing in the Clipboard or trying to paste a table within a table cell. Word won't do either task, but it will continue trying until it is done with all the cells in the selection.

You should note that this macro replaces whatever is in the selected cells with the contents of the Clipboard; whatever was previously in the cells is lost. If you want to instead add information to the beginning of the cells, without disturbing the existing contents of the cell, you could use this slightly modified macro:

Sub PasteToCellsStart()
    Dim TargetRange As Range
    Dim oTargCell As Cell
    Dim PasteRange As Range

    If Selection.Cells.Count = 0 Then
        'Quit if no cells in selection
        MsgBox "No cells selected", vbCritical
        Exit Sub
    End If
    On Error Resume Next
    Set TargetRange = Selection.Range
    For Each oTargCell In Selection.Cells
        Set PasteRange = oTargCell.Range
        PasteRange.Collapse wdCollapseStart
        PasteRange.Paste
    Next oTargCell
    TargetRange.Select
End Sub

One last modification would be to come up with a macro that would paste to the end of what is in the cells. You might think that you could replace wdCollapseStart with wdCollapseEnd in the foregoing macro, but that doesn't work properly within tables. Instead, you must replace the For ... Next loop in the above macro. The following example shows a changed version of the macro.

Sub PasteToCellsEnd()
    Dim TargetRange As Range
    Dim oTargCell As Cell
    Dim PasteRange As Range

    If Selection.Cells.Count = 0 Then
        'Quit if no cells in selection
        MsgBox "No cells selected", vbCritical
        Exit Sub
    End If
    On Error Resume Next
    Set TargetRange = Selection.Range
    For Each oTargCell In Selection.Cells
        Set PasteRange = oTargCell.Range.Characters.Last
        PasteRange.Collapse wdCollapseStart
        PasteRange.Paste
    Next oTargCell
    TargetRange.Select
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13157) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Filling Table Cells with a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Shifting Margins Evident in Word 2002

When you open a document in one version of Word and compare it to what you see for the same document in a different ...

Discover More

Vertically Centering Labels

Want the text printed on your labels to be centered vertically? It's not that hard, and this tip shows the easiest method.

Discover More

Changing Your Windows 8 Account Picture

Want to personalize your Windows 8 experience a bit more? One way is by attaching a picture to your account, as discussed ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Keeping Table Rows Together

When you create a table that extends beyond a single page, you may want to make sure that the information in a table row ...

Discover More

Using Outline Numbering in a Table

Can you put a numbered outline in a table? Yes, you can. But Word is rather prickly when it comes to using the keyboard ...

Discover More

Setting a Default Table Border Width

When you insert a table into your document, it uses a standard-weight line around each cell in the table. If you want to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 8?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.