Re: help with Table Macro



On Thu, 26 Jun 2008 17:21:24 -0700 (PDT), Cesar <cdelanoval@xxxxxxxxx> wrote:

On Jun 26, 4:59 pm, "Jay Freedman" <jay.freed...@xxxxxxxxxxx> wrote:
Cesar wrote:
On Jun 26, 2:31 pm, "Jay Freedman" <jay.freed...@xxxxxxxxxxx> wrote:
Cesar wrote:
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.

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        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
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

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 newsgroup so all may benefit.
.



Relevant Pages

  • Re: help with Table Macro
    ... Available Float, which will always be in column ... string contained in that cell. ... test phrase "available float". ... next matching word and repeat the process until no more matching words ...
    (microsoft.public.word.application.errors)
  • Re: help with Table Macro
    ... Available Float, which will always be in column ... string contained in that cell. ... test phrase "available float". ... next matching word and repeat the process until no more matching words ...
    (microsoft.public.word.application.errors)
  • Re: help with Table Macro
    ... "available float")> 0 Then ... string contained in that cell. ... comparison to the test phrase "available float". ... originally provided for was looking for the matching words in row 7, ...
    (microsoft.public.word.application.errors)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: inconsistent behavior of >FLOAT
    ... empty string, strings with leading and trailing blanks. ... I think it is too late for>FLOAT to rescind the strong suggestion in its spec that this "should" be the case. ... As long as the spec is tightened to require that a string of blanks must be treated as a valid zero, a programmer would know to filter such a case, if needed, before it is passed to>FLOAT. ... I think ignoring leading and trailing blanks may be reasonable for>FLOAT, as long as it does not violate the current spec. ...
    (comp.lang.forth)

Loading