Reply
 
Thread Tools Display Modes
  #1   Report Post  
KWC via OfficeKB.com
 
Posts: n/a
Default Deleting blank rows in all tables

I need a macro that will look at all tables in a document, then delete any
rows in those tables where the entire row is blank.

This is just a start, and does not work. Would someone provide some guidence?
thanks in advance.

Option Explicit
Sub delete_blank_rows()
Dim odoc1 As Document
Dim lngRow As Long
Dim oTable As Table
Dim orow As Row

Selection.HomeKey Unit:=wdStory
If ActiveDocument.Tables.Count = 1 Then
Set odoc1 = ActiveDocument
For Each oTable In odoc1.Tables

For lngRow = oTable.Rows.Count To 1 Step -1
If Selection.Text = "" Then
oTable.Rows(lngRow).Delete
End If
Next lngRow

Next oTable
End If
End Sub
  #2   Report Post  
KWC via OfficeKB.com
 
Posts: n/a
Default

Here is what I ended up doing and it works. Got help from the WOPR board.

Function RowIsBlank(r As Word.Row) As Boolean
If Len(Replace(r.Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then ' Chr
13 and 7 are paragraph marker and end cell marker
RowIsBlank = True 'row contain no text
Else
RowIsBlank = False 'row contains some text
End If
End Function

Sub Delete_Blank_Rows()
Dim odoc1 As Document
Dim oTable As Table
Dim rw As Word.Row
Dim lngrow As Integer
Dim row_cnt As Integer

MacroEntry
Selection.HomeKey Unit:=wdStory
If ActiveDocument.Tables.Count = 1 Then
Set odoc1 = ActiveDocument
For Each oTable In odoc1.Tables
On Error Resume Next
For lngrow = oTable.Rows.Count To 1 Step -1
On Error Resume Next
If RowIsBlank(oTable.Rows(lngrow)) Then
oTable.Rows(lngrow).Delete
If Err.Number = 0 Then
row_cnt = row_cnt + 1
End If
End If
Next lngrow
Next oTable
End If
MacroExit
Selection.HomeKey Unit:=wdStory
MsgBox "Finished - " & row_cnt & " blank rows were deleted"
End Sub

Sub MacroEntry()
Sbar = Application.DisplayStatusBar
Application.DisplayStatusBar = True
Application.ScreenUpdating = False
End Sub

Sub MacroExit()
Application.StatusBar = False
Application.DisplayStatusBar = Sbar
Application.ScreenUpdating = True
End Sub

--
Message posted via http://www.officekb.com
  #3   Report Post  
Jay Freedman
 
Posts: n/a
Default

Hi KWC,

Yes, that works. Just one nitpick: The two lines that say

Selection.HomeKey Unit:=wdStory

should be removed. There's no reason to move the Selection (the insertion
point) to the beginning of the document, since the rest of the macro doesn't
depend at all on where the insertion point is. And there's certainly no
reason to do it twice!

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

KWC via OfficeKB.com wrote:
Here is what I ended up doing and it works. Got help from the WOPR
board.

Function RowIsBlank(r As Word.Row) As Boolean
If Len(Replace(r.Range.Text, Chr(13) & Chr(7), vbNullString)) = 0
Then ' Chr 13 and 7 are paragraph marker and end cell marker
RowIsBlank = True 'row contain no text
Else
RowIsBlank = False 'row contains some text
End If
End Function

Sub Delete_Blank_Rows()
Dim odoc1 As Document
Dim oTable As Table
Dim rw As Word.Row
Dim lngrow As Integer
Dim row_cnt As Integer

MacroEntry
Selection.HomeKey Unit:=wdStory
If ActiveDocument.Tables.Count = 1 Then
Set odoc1 = ActiveDocument
For Each oTable In odoc1.Tables
On Error Resume Next
For lngrow = oTable.Rows.Count To 1 Step -1
On Error Resume Next
If RowIsBlank(oTable.Rows(lngrow)) Then
oTable.Rows(lngrow).Delete
If Err.Number = 0 Then
row_cnt = row_cnt + 1
End If
End If
Next lngrow
Next oTable
End If
MacroExit
Selection.HomeKey Unit:=wdStory
MsgBox "Finished - " & row_cnt & " blank rows were deleted"
End Sub

Sub MacroEntry()
Sbar = Application.DisplayStatusBar
Application.DisplayStatusBar = True
Application.ScreenUpdating = False
End Sub

Sub MacroExit()
Application.StatusBar = False
Application.DisplayStatusBar = Sbar
Application.ScreenUpdating = True
End Sub



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Blank spaces between tables in word Sal F Tables 1 March 8th 05 07:29 PM
disalignment of "header row(s)" in tables? mylory2 Tables 0 February 24th 05 09:07 PM
Word tables should permit header rows to repeat only on every oth. vepxistqaosani Tables 4 February 24th 05 05:53 AM
Splitting Rows of tables across page breaks Barb Reinhardt Microsoft Word Help 2 February 22nd 05 02:09 AM
Blank spaces between rows Hélène Tables 9 February 1st 05 04:44 AM


All times are GMT +1. The time now is 12:36 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"