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!

MS Word Delete Row

Status
Not open for further replies.

Drago1920

Technical User
Jan 8, 2017
6
0
0
US
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.
 
Hi,

What code do you have currently?

What code have you tried?

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

When I enter the data in the last cell in that row, and do [tt]MoveRight Unit:=wdCell[/tt] 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.
 
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]
 
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.

table_e8ozua.gif
 
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
 
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 [highlight #FCE94F]<<<<<<<<< CHANGED THIS TO THE THIRD ROW[/highlight]

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
 
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]
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top