help with Table Macro

C

Cesar

I am new into writing macros and scripts and I need help with a
simple
procedure.

I have a word document that contains a table. The table has several
rows and 7 columns. (The number of rows varies, but the columns are
always 7)


Within this table I need script that will have a certain word or
phrase (for example: Available Float, which will always be in column
7). Once the word is found, I need the script to delete the whole
row, and go to the next looking for the same ( loop ).


I think I know how to do a macro to find the word, I just don't know
how to continue to to the rest ( clear contents with the row, select
the row and delete the row).


Your help would be greatly appreciated.


Thank you
 
J

Jay Freedman

Cesar said:
I am new into writing macros and scripts and I need help with a
simple
procedure.

I have a word document that contains a table. The table has several
rows and 7 columns. (The number of rows varies, but the columns are
always 7)


Within this table I need script that will have a certain word or
phrase (for example: Available Float, which will always be in column
7). Once the word is found, I need the script to delete the whole
row, and go to the next looking for the same ( loop ).


I think I know how to do a macro to find the word, I just don't know
how to continue to to the rest ( clear contents with the row, select
the row and delete the row).


Your help would be greatly appreciated.


Thank you

First the code, then the explanation...

Sub demo()
Dim myTable As Table
Dim myRowNum As Long

If ActiveDocument.Tables.Count = 0 Then
MsgBox "No tables in the document"
Exit Sub
End If

Set myTable = ActiveDocument.Tables(1)

For myRowNum = myTable.Rows.Count To 1 Step -1
If InStr(LCase(myTable.Cell(myRowNum, 7).Range.Text), _
"available float") > 0 Then
myTable.Rows(myRowNum).Delete
End If
Next
End Sub

The stuff up to the For loop should be obvious. Of course, you can set
myTable to any table in the document, not necessarily the first one.

The For loop runs through each row in the table from the last one to the
first one. It runs "backwards" because some of the rows may be deleted, and
when that happens Word automatically renumbers the remaining rows. If you
were looping from the first row to the last, that would cause some rows to
be missed, and you'd hit an error when the loop counter becaomes larger than
the remaining number of rows.

Inside the loop the If statement is rather complicated. Analyze it from the
innermost parentheses outward:

- The expression myTable.Cell(myRowNum, 7) points to the cell in column 7 of
the row that's currently being examined.

- The expression myTable.Cell(myRowNum, 7).Range.Text gives the string
contained in that cell.

- The LCase function is applied to that string, changing it to all
lower-case letters; that allows a case-insensitive comparison to the test
phrase "available float". (A better idea would be to assign the test phrase
to a String variable, which you could change more easily.)

- The InStr function compares the cell-content string to the test string. If
the result is 0 then the test string doesn't occur anywhere inside the
cell-content string; if it's greater than 0, then it does occur.

When the condition in the If statement is true (that is, the cell does
contain the test string), then the line myTable.Rows(myRowNum).Delete does
everything necessary to remove the row. Note that you do _not_ have to
delete the contents separately or select the row in order to delete it.

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

Cesar

First the code, then the explanation...

Sub demo()
    Dim myTable As Table
    Dim myRowNum As Long

    If ActiveDocument.Tables.Count = 0 Then
        MsgBox "No tables in the document"
        Exit Sub
    End If

    Set myTable = ActiveDocument.Tables(1)

    For myRowNum = myTable.Rows.Count To 1 Step -1
        If InStr(LCase(myTable.Cell(myRowNum, 7).Range.Text), _
                "available float") > 0 Then
            myTable.Rows(myRowNum).Delete
        End If
    Next
End Sub

The stuff up to the For loop should be obvious. Of course, you can set
myTable to any table in the document, not necessarily the first one.

The For loop runs through each row in the table from the last one to the
first one. It runs "backwards" because some of the rows may be deleted, and
when that happens Word automatically renumbers the remaining rows. If you
were looping from the first row to the last, that would cause some rows to
be missed, and you'd hit an error when the loop counter becaomes larger than
the remaining number of rows.

Inside the loop the If statement is rather complicated. Analyze it from the
innermost parentheses outward:

- The expression myTable.Cell(myRowNum, 7) points to the cell in column 7of
the row that's currently being examined.

- The expression myTable.Cell(myRowNum, 7).Range.Text gives the string
contained in that cell.

- The LCase function is applied to that string, changing it to all
lower-case letters; that allows a case-insensitive comparison to the test
phrase "available float". (A better idea would be to assign the test phrase
to a String variable, which you could change more easily.)

- The InStr function compares the cell-content string to the test string.If
the result is 0 then the test string doesn't occur anywhere inside the
cell-content string; if it's greater than 0, then it does occur.

When the condition in the If statement is true (that is, the cell does
contain the test string), then the line myTable.Rows(myRowNum).Delete does
everything necessary to remove the row. Note that you do _not_ have to
delete the contents separately or select the row in order to delete it.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso
all may benefit.- Hide quoted text -

- Show quoted text -

Jay, Thank you for the reply but I couldn't get it to work. What it
did was to find the matching word on column number 7, and then it
deleted column number 7.

What I actually want to do is to find the matching words in the table
(which are always somewhere in column number 7, and once found, I like
to delete the row ( not the column ), and then loop again to find the
next matching word and repeat the process until no more matching words
are founded

i do appreciate you help very much

Thank you
 
J

Jay Freedman

Cesar said:
Jay, Thank you for the reply but I couldn't get it to work. What it
did was to find the matching word on column number 7, and then it
deleted column number 7.

What I actually want to do is to find the matching words in the table
(which are always somewhere in column number 7, and once found, I like
to delete the row ( not the column ), and then loop again to find the
next matching word and repeat the process until no more matching words
are founded

i do appreciate you help very much

Thank you

Did you paste the macro directly into the VBA editor and run it, or did you
retype it (and maybe make a mistake)?

There is absolutely no way that the statement

myTable.Rows(myRowNum).Delete

could delete a column. Notice the word "Rows" in the statement...

If you're sure your code matches what I posted, and it really does delete a
column -- a vertical sequence of cells -- from the table, I want to see your
document and the code. Please zip it and send it to my email address.

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

Cesar

Did you paste the macro directly into the VBA editor and run it, or did you
retype it (and maybe make a mistake)?

There is absolutely no way that the statement

   myTable.Rows(myRowNum).Delete

could delete a column. Notice the word "Rows" in the statement...

If you're sure your code matches what I posted, and it really does deletea
column -- a vertical sequence of cells -- from the table, I want to see your
document and the code. Please zip it and send it to my email address.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso
all may benefit.- Hide quoted text -

- Show quoted text -

Sorry about the confusion. I did make some changes and that's how I
got column 7 deleted. I did copy and paste your code and it didn't
work as planned so I changed it a bit. Anyway, I think the code as
originally provided for was looking for the matching words in row 7,
instead of column 7.

Let me explain in more detail what I need. This is word document that
will contain 1 table only. This table has a fixed number of columns
( 7 ), but the number of rows will always be different (varies).

In differrent rows, without any specfic sequence, the words "Available
float" will be shown. This will always be found in column 7.

I need a code that searches for the first available cell containing
the words "Available float". Once found, I need the row where these
matching words are found to be deleted. Then I need the code to loop
again until no more matches are found.

Thank you for the great help
 
J

Jay Freedman

Sorry about the confusion. I did make some changes and that's how I
got column 7 deleted. I did copy and paste your code and it didn't
work as planned so I changed it a bit. Anyway, I think the code as
originally provided for was looking for the matching words in row 7,
instead of column 7.

Let me explain in more detail what I need. This is word document that
will contain 1 table only. This table has a fixed number of columns
( 7 ), but the number of rows will always be different (varies).

In differrent rows, without any specfic sequence, the words "Available
float" will be shown. This will always be found in column 7.

I need a code that searches for the first available cell containing
the words "Available float". Once found, I need the row where these
matching words are found to be deleted. Then I need the code to loop
again until no more matches are found.

Thank you for the great help

The original code does exactly what you have explained (twice). I tested it
before I posted it, and it worked correctly.

I repeat my offer: send me a copy of your document, and I'll tell you what I
find.
 
C

Cesar

The original code does exactly what you have explained (twice). I tested it
before I posted it, and it worked correctly.

I repeat my offer: send me a copy of your document, and I'll tell you what I
find.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

Jay, I am sorry for everything. I was able to figure out my
problem. You are right the code works perfect.

Thank you
 

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