Need Help With Deleting TableRows

J

JBNewsGroup

Hi,

I am working in WORD2000 VBA.

I need help and / or an explanation of the following Table procedure. I am
either doing something wrong or I am missing a point someplace. I have the
following procedure:

Private Sub DeleteTableRows (oTable As Table, _
RowsToDelete() as Integer, _
NoOfDeletes As Integer)
Dim oRow as Range
Dim J As Integer
Dim RowNo As Integer

Debug.Print "On Entry: " & oTable.Rows.Count

For J = NoOfDeletes To 1 Step -1
RowNo = RowsToDelete (J)
Set oRow = oTable.Rows (RowNo).Range
oRow.Delete
Next J

Debug.Print "On Exit: " & oTable.Rows.Count
End Sub

On entry to the procedure the first Debug.Print shows that I have a table of
50 rows. After executing 5 deletes the second Debug.Print shows that I
still have a table of 50 Rows instead of 45 rows. If I call the routine a
second time (ignoring the row count) only the row text is deleted for the
rows defined for the second call. The rows do not seem to be deleted as in
the first call. Note: prior to calling the procedure I execute "Set oTable
= ActiveDocument.Tables(TableNo)". I define oTable only once for all the
procedure calls.

Any help and explanation of what is occurring will be greatly appreciated.

Thanks in advance for any help.

Jerry Bodoff
 
C

Chuck

Hi Jerry

There's a difference between
oTable.Rows(RowNo).Range.Delete
and
oTable.Rose(RowNo).Delete

The first deletes the contents of the row but not the row itself, the second
deletes the row itself and its contents. Since your code is not removing the
row from the table it makes sense your row count wouldn't change.

Just curious, why are you defining oRow just to delete it? Since you don't
use oRow for anything else you can skip setting the value of that object and
just delete the row in one line:
oTable.Rows(RowNo).Range.Delete
or
oTable.Rows(RowNo).Delete

Chuck
 
J

JBNewsGroup

Hi Chuck,

Thanks for the explanation of why things are different.

I am writing this for a friend of mine and in the interests of clarity I was
thinking his way. Then again, if he understands the way I have it he should
be able to understand the other. That is the only reason for defining it
that way. I am changing the code to the one-liner.

Jerry Bodoff
 

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