×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

MS Word Delete Row

MS Word Delete Row

MS Word Delete Row

(OP)
First I want to thank anyone that can assist me with what will probably be a very simple code, but I am having trouble locating the solution.

I have a macro enabled word document with a userform that will populate bookmarks in a table. The table has 30 rows and 4 columns. If the user only needs half of the bookmarks the remaining table is blank and I need to delete the un-used rows (content and all) when the forth column is blank.

I have tried several codes, and to no avail they are not what I am looking for. Any thoughts. Again, thank you.

RE: MS Word Delete Row

Hi,

What code do you have currently?

What code have you tried?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: MS Word Delete Row

When I have this situation, I usually have just one Bookmark in the very first cell in my table.
|-----------|-----------|-----------|-----------|
} [MyBookM] |           |           |           |
}-----------|-----------|-----------|-----------| 
When I .GoTo What:=wdGoToBookmark, Name:="MyBookM" and enter the data, I can then do MoveRight Unit:=wdCell and enter the data in the next cell.

When I enter the data in the last cell in that row, and do MoveRight Unit:=wdCell I get another row of cells in my table and continue on. No additional bookmarks needed, just one.
So I create the rows as I need them, and I don't have to Delete the unused rows when I am done.


---- Andy

There is a great need for a sarcasm font.

RE: MS Word Delete Row

It seems to me you're going about this the wrong way - you code should be adding rows as needed and there is probably no need for any bookmarks. That said, for code to delete empty rows, try:

CODE

Sub DeleteEmptyRows()
Dim r As Long
With ActiveDocument.Tables(1)
  For r = .Rows.Count To 1 Step -1
    With .Rows(r)
      If Len(.Range.Text) = .Cells.Count * 2 + 2 Then .Delete
    End With
  Next r
End With
End Sub 
where '1' in ActiveDocument.Tables(1) in the number of the table to process.

Cheers
Paul Edstein
[MS MVP - Word]

RE: MS Word Delete Row

(OP)
Thank you all for replying, i guess i should have explained a little better. The form that is being used is a legal document in order to obtain a search warrant. The userform that i created has 15 boxes for any charged needed to be listed and 15 boxes for the statutes and the user fills in only what they need. then inside the word doc each charge and statute has to be listed in four different places with in the paragraph and in the tables i set up. that is why i need to remove any un-used bookmarks within the table and document. i attached a pic of the table.

The table is set up with 4 columns 1) column is a spacer, 2) is the Title, 3) is a spacer, and 4) is the bookmark. I need the script to search for any empty text in column 4 and then delete that row, and then move on to the next empty spot. Sorry for the layout, but these Judges are very picky on how their documents are layout.

Macropod i tried your code and it did not delete any of the rows. I will try to find one of the codes that somewhat worked and maybe we can tweak it to work for this case.

Thanks again for all your assistance.



RE: MS Word Delete Row

(OP)
Here is the code i was trying to make work, but it keeps deleting all the rows from the third one down no matter if it was blank or not.

Application.ScreenUpdating = False
Dim Tbl As Table
Dim i As Long
Dim noOfCol As Integer, j As Integer

For Each Tbl In ThisDocument.Tables
With Tbl
noOfCol = Tbl.Range.Rows(2).Cells.Count

For i = .Rows.Count To 3 Step -1
With .Rows(i)
If Len(.Range) = noOfCol * 2 + 2 Then .Delete
End With
Next i
With .Range
For i = .Cells.Count To noOfCol * 4 + 1 Step -1
On Error Resume Next
If Len(.Cells(i).Range) = 2 Then
.Rows(.Cells(i).RowIndex).Delete
j = i Mod noOfCol
If j = 0 Then j = noOfCol
i = i - j
End If
Next i
End With
End With
Next Tbl
Set Tbl = Nothing
Application.ScreenUpdating = True

RE: MS Word Delete Row

(OP)
OK guys i got this code to work for me the way in needed with a slight change. Thank you all again for the assistance...



Application.ScreenUpdating = False
Dim Tbl As Table
Dim i As Long
Dim noOfCol As Integer, j As Integer

For Each Tbl In ThisDocument.Tables
With Tbl
noOfCol = Tbl.Range.Rows(3).Cells.Count [highlight <<<<<<<<< CHANGED THIS TO THE THIRD ROW

For i = .Rows.Count To 3 Step -1
With .Rows(i)
If Len(.Range) = noOfCol * 2 + 2 Then .Delete
End With
Next i
With .Range
For i = .Cells.Count To noOfCol * 3 + 1 Step -1
On Error Resume Next
If Len(.Cells(i).Range) = 2 Then
.Rows(.Cells(i).RowIndex).Delete
j = i Mod noOfCol
If j = 0 Then j = noOfCol
i = i - j
End If
Next i
End With
End With
Next Tbl
Set Tbl = Nothing
Application.ScreenUpdating = True

RE: MS Word Delete Row

Quote:

Macropod i tried your code and it did not delete any of the rows.
That's because your rows aren't empty - which is what one might reasonably have understood from your description of them as unused. Your screenshot, however, suggests column 1 is always in-use (because it's never empty). Accordingly, try:

CODE

Sub DeleteUnusedRows()
Dim r As Long, Rng As Range
With ActiveDocument.Tables(1)
  For r = .Rows.Count To 1 Step -1
    With .Rows(r)
      Set Rng = .Range
      Rng.Start = .Cells(2).Range.Start
      If Len(Rng.Text) = Rng.Cells.Count * 2 + 2 Then .Delete
    End With
  Next r
End With
Set Rng = Nothing
End Sub 

Cheers
Paul Edstein
[MS MVP - Word]

RE: MS Word Delete Row

So I guess if someone has 16 charges or more, he has to be send home. ponder


---- Andy

There is a great need for a sarcasm font.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close