User form / vba / table

G

Guest

This is my first post here, so greetings everybody.

I am making a user form to collect various client/order details and insert
them into a two page document. My vba knowledge is limited, and so far I've
been relying on google and some offline manuals for assistance. As I have a
few questions with regard to this form perhaps someone here will be able to
help.

In the document I have three tables. I need the following piece of code to
operate on the second table:

Dim oRow As Row

For Each oRow In Selection.Tables(1).Rows
If oRow.Cells(4).Range.Text = vbCr & Chr(7) Then oRow.Delete
Next oRow

Right now, if I execute it I get: runtime error 5941, the requested member
of the collection does not exist.

Any suggestions?

Sergey
 
J

Jay Freedman

This is my first post here, so greetings everybody.

I am making a user form to collect various client/order details and insert
them into a two page document. My vba knowledge is limited, and so far I've
been relying on google and some offline manuals for assistance. As I have a
few questions with regard to this form perhaps someone here will be able to
help.

In the document I have three tables. I need the following piece of code to
operate on the second table:

Dim oRow As Row

For Each oRow In Selection.Tables(1).Rows
If oRow.Cells(4).Range.Text = vbCr & Chr(7) Then oRow.Delete
Next oRow

Right now, if I execute it I get: runtime error 5941, the requested member
of the collection does not exist.

Any suggestions?

Sergey

There are actually two places where that error message could occur, first on the
expression Selection.Tables(1) and second on the expression oRow.Cells(4). If
the VBA editor appears with one of those lines highlighted in yellow, that will
tell you which expression is the cause.

I'll guess that the culprit is probably Selection.Tables(1), and that the reason
is that the selection (the insertion point or selected text) does not contain
and is not contained in a table. Therefore, the collection Selection.Tables is
empty.

If you know that it's always the second table in the document that needs to be
worked on, then throw away the reference to Selection and just write

For Each oRow In ActiveDocument.Tables(2).Rows

Further, if there's any possibility that the user or your code has removed the
table entirely, then your code should first verify that the table exists and is
the correct one (how you do that depends on what is unique about the table).
 
G

Guest

Thank you very much indeed.

I'll give that a try but not just now.

And I have a couple of other things to ask about this user form.

Sergey
 
G

Guest

If you know that it's always the second table in the document that needs
to be
worked on, then throw away the reference to Selection and just write

For Each oRow In ActiveDocument.Tables(2).Rows

This worked.

Thanks again.

Sergey
 

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