wordsite.com

   
 


Frequently Asked Questions about Macros

Introduction to VBA Programming

Programming fundamentals

Returning information

Working with tables

Miscellaneous routines

New Routines 1999-2000

Microsoft is a registered trademark of Microsoft Corporation
 
 

   


Working with tables

Click a topic:

Apply changes to all cells in a table.

Apply changes to an individual cell in a table.

Select all but the first two cells in a table column.

Display in a message box the contents of each cell in a table column.

Select a range of cells within a table.

Select all rows of a table except the first row.

Delete all rows of a table that contain a particular text string in the first column.

Apply changes to all cells in a table. <Top of Page>

Solution:
Drill down to the cells in the table's range, as follows, and set the various properties as desired:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
For Each oCell In oTable.Range.Cells
oCell.Width = InchesToPoints(1)
oCell.Shading.BackgroundPatternColorIndex = wdBlue
oCell.Range.Font.Name = "Arial"
oCell.Range.Font.Size = 20
Next oCell

Apply changes to an individual cell in a table. <Top of Page>

Solution:
Drill down to the desired cell. In this example, Cell(1, 1) refers to the cell in row 1, column 1:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
With ActiveDocument.Tables(1).Cell(1, 1)
.Width = InchesToPoints(1)
.Shading.BackgroundPatternColorIndex = wdBlue
.Range.Font.Name = "Arial"
.Range.Font.Size = 20
End With

Select all but the first two cells in a table column. <Top of Page>

Solution:
After selecting the column, set the start of the selection range to the start of the third cell in the selection. Wrap the code inside an If statement to make sure the selection is within a table. (That way, if not in a table, nothing will happen.)

Code:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
If Selection.Information(wdWithInTable) Then
Selection.Columns(1).Select
Selection.SetRange _
Start:=Selection.Cells(3).Range.Start, _
End:=Selection.End
End If

Display in a message box the contents of each cell in a table column. <Top of Page>

Solution:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
Sub DisplayTextFromCellsInColumn1()
For Each oCell In Selection.Tables(1).Columns(1).Cells
Set myRange = oCell.Range
myRange.SetRange Start:=myRange.Start, End:=myRange.End - 1
MsgBox myRange.Text
Next oCell
End Sub

Select a range of cells within a table. <Top of Page>

Solution:
Use the Selection.SetRange statement

Notes:
The sample provided here selects all cells from row 2 column 2 to row 3 column 3.

Code:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
Sub SelectRangeOfCells()
If Selection.Information(wdWithInTable) = False Then Exit Sub
Selection.SetRange _
Start:=Selection.Tables(1).Cell(2, 2).Range.Start, _
End:=Selection.Tables(1).Cell(3, 3).Range.End
End Sub

Select all rows of a table except the first row. <Top of Page>

Solution:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
ActiveDocument.Tables(1).Select
Selection.SetRange _
Start:=Selection.Rows(2).Range.Start, _
End:=Selection.End

Delete all rows of a table that contain a particular text string in the first column. <Top of Page>

Solution:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
Sub DeleteRows()
If Selection.Information(wdWithInTable) = False Then Exit Sub
TargetText = InputBox$("Enter target text:", "Delete Rows")
For Each oRow In Selection.Tables(1).Rows
If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then oRow.Delete
Next oRow
End Sub