Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through a word doc to delete rows

Status
Not open for further replies.

davefish

Technical User
Jul 26, 2002
169
0
0
GB
I have a word table that is populated by Excel VBA as shown below:-

Code:
wordDoc.Tables(4).Cell(2, 1).Range.Text = "    " & Range("O19").Value & "   Way Flush Mounting, Programmable Alarm Annunciator." 'Product code
    wordDoc.Tables(4).Cell(5, 1).Range.Text = Range("O25").Value 'Window size
    wordDoc.Tables(4).Cell(8, 1).Range.Text = Range("O13").Value & "W" & Range("O14").Value & "H" 'Panel Size
    wordDoc.Tables(4).Cell(12, 1).Range.Text = Range("O26").Value 'Pushbutton
    wordDoc.Tables(4).Cell(14, 1).Range.Text = Range("O19").Value & "A" 'Channel Count
    wordDoc.Tables(4).Cell(15, 1).Range.Text = Range("P24").Value ' Unarmed
    wordDoc.Tables(4).Cell(16, 1).Range.Text = Range("R25").Value 'Illumination
    wordDoc.Tables(4).Cell(17, 1).Range.Text = Range("O27").Value 'RR
    wordDoc.Tables(4).Cell(18, 1).Range.Text = Range("R26").Value 'Trop
    wordDoc.Tables(4).Cell(19, 1).Range.Text = Range("O29").Value 'FCV
    wordDoc.Tables(4).Cell(20, 1).Range.Text = Range("P33").Value 'Comms
    wordDoc.Tables(4).Cell(21, 1).Range.Text = Range("P32").Value 'GFI
    wordDoc.Tables(4).Cell(22, 1).Range.Text = Range("L44").Value 'Logo
    wordDoc.Tables(4).Cell(24, 1).Range.Text = Range("O28").Value 'SEC
    wordDoc.Tables(4).Cell(29, 1).Range.Text = Range("Q76").Value 'Option
    wordDoc.Tables(4).Cell(30, 3).Range.Text = Range("B3").Value 'Part number


In some instances it places an "X" in column 1 as shown below
https://res.cloudinary.com/engineering-com/image/upload/v1664898803/tips/word_table_z0jjzg.jpg

I use the following code to successfully to delete other rows with the "X", but it is failing to delete these two and I cannot fathom why. Any help apprecaited.

[codeWith wordDoc.Tables(4)
For r = 1 To .Rows.Count
If .Cell(r, c).Range.Text Like "*" & "X" & "*" Then
wordDoc.Tables(4).Rows(r).Delete
End If
Next r
End With[/code]
 
Without going dipper into it, have you tried to do it backwards:

Code:
With wordDoc.Tables(4)
   For r = [red].Rows.Count To 1[/red] Step -1   [green]'Thank you strongm :-)[/green]
       If .Cell(r, c).Range.Text Like "*" & "X" & "*" Then
           wordDoc.Tables(4).Rows(r).Delete
       End If
   Next r
End With

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
davefish said:
I use the following code to successfully to delete other rows with the "X"

From the provided code ([tt]Like "*" & "X" & "*"[/tt]), it looks to me that you delete the rows when the cell contains X, so not only X by itself, but also "Malcolm X", "X-Factor", "ABCXYZ", etc.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
@Andrzejek
@strongm

Great job! Thank you both for your suggestion it worked straight away
 
davefish,
If one or more of the posts helped you with your issue, consider clicking the "Great post!" link.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Hmmmm?

Out of 169 posting encounters on Tek-Tips, the OP had expressed with a little purple star only twice in 20 years.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!
 
Simpler:
Code:
With wordDoc.Tables(4)
  For r = .Rows.Count To 1 Step -1
    If Split(.Cell(r, 1).Range.Text, vbCr)(0) = "X" Then .Rows(r).Delete
  Next r
End With

Cheers
Paul Edstein
[Fmr MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top