codewala

code it

Macro to delete empty rows from a table in word


Sub DeleteEmptyRowsFromTable()
'
Dim oDoc As Document
Dim oTable As Table
Dim oRow As Row
Dim totalRows As Long
Dim textInCell As String
Dim i As Long
'
Set oDoc = ActiveDocument
Set oTable = oDoc.Tables(1)
'
totalRows = oTable.Rows.Count
'
For i = totalRows To 1 Step -1
Set oRow = oTable.Rows(i)
textInCell = CleanText(oRow.Cells(1).Range.Text)
'
If textInCell = "" Then
oRow.Delete
End If
Next i
'
End Sub
'
'
'
Function CleanText(pm_str As String) As String
'
pm_str = Trim(pm_str)
pm_str = Replace$(pm_str, "", "")
pm_str = Replace$(pm_str, Chr(13), "") ' vbCrLf (return)
pm_str = Replace$(pm_str, Chr(7), "") ' vbCrLf (return)
CleanText = pm_str
'
End Function

Advertisements

December 24, 2010 - Posted by | VBA, Word

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: