Cells Collection Object

Microsoft Word Visual Basic

Multiple objectsCells
Multiple objects

A collection of Cell objects in a table column, table row, selection, or range.

Using the Cells Object

Use the Cells property to return the Cells collection. The following example formats the cells in the first row in table one in the active document to be 30 points wide.

ActiveDocument.Tables(1).Rows(1).Cells.Width = 30
		

The following example returns the number of cells in the current row.

num = Selection.Rows(1).Cells.Count
		

Use the Add method to add a Cell object to the Cells collection. You can also use the InsertCells method of the Selection object to insert new cells. The following example adds a cell before the first cell in myTable.

Set myTable = ActiveDocument.Tables(1)
myTable.Range.Cells.Add BeforeCell:=myTable.Cell(1, 1)
		

Use Cell(row, column), where row is the row number and column is the column number, or Cells(index), where index is the index number, to return a Cell object. The following example applies shading to the second cell in the first row in table one.

Set myCell = ActiveDocument.Tables(1).Cell(Row:=1, Column:=2)
myCell.Shading.Texture = wdTexture20Percent
		

The following example applies shading to the first cell in the first row.

ActiveDocument.Tables(1).Rows(1).Cells(1).Shading _
    .Texture = wdTexture20Percent
		

Remarks

Use the Add method with the Rows or Columns collection to add a row or column of cells. The following example adds a column to the first table in the active document and then inserts numbers into the first column.

Set myTable = ActiveDocument.Tables(1)
Set aColumn = myTable.Columns.Add(BeforeColumn:=myTable.Columns(1))
For Each aCell In aColumn.Cells
    aCell.Range.Delete
    aCell.Range.InsertAfter num + 1
    num = num + 1
Next aCell