Help with clearing some table cell contents

C

Chris Lewis

What I need to do is if I have a table of rows x. Each row
can contain either 1, 2, 4 or 5 cells. I need to loop through each row in
the table and clear the contents of the Cells 3, 4 and 5 only in the rows
with 5 cells. all other rows are left alone.

so

Selection.Tables(1).Select
j = Selection.Rows.Count ' i need the number of rows later so stored in
variable now
For rownum = 1 To j
Selection.Tables(1).Rows(rownum).Select

If Selection.Cells.Count = 5 Then

Something here to delete the contents of cells 3, 4 and 5 of the current
row#

Next rownum

any ideas?
 
J

Jay Freedman

Hi Chris,

Don't select anything. Use a Range object. When a row has 3 or more
cells, set the Range object to the range of the 3rd cell and then
extend it to cover the rest of the cells to the end of the row. The
..Delete method will then remove any text from those cells (it doesn't
delete the cells themselves).

Sub Demo()
Dim oRg As Range
Dim oRow As Row
Dim j As Long

j = ActiveDocument.Tables(1).Rows.Count

For Each oRow In ActiveDocument.Tables(1).Rows
If oRow.Cells.Count > 2 Then
Set oRg = oRow.Cells(3).Range
oRg.End = oRow.Range.End
oRg.Delete
End If
Next
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top