Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Filling Table Cells with a Macro.
Written by Allen Wyatt (last updated April 19, 2021)
This tip applies to Word 97, 2000, 2002, and 2003
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:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1508) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Filling Table Cells with a Macro.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
If you import information into a document from another program, the values you import may not be exactly to your liking. ...
Discover MoreOne of the most common ways to format information in a table is to apply some sort of alignment to the contents of table ...
Discover MoreWhen pasting text from another document or from the Web you can have unexpected characters sometimes show up. Many of ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments