Deleting cells with VBA

M

Morales

Hi All, I have already posted this message once, but I can't see it.
If it pops up later - apologies.
Can any of you gurus tell me how to select and delete a row of cells
using VBa if the row is empty. In most cases the tables will be one
cell deep by two cells wide. Thanks in advance.
 
M

macropod

Hi Morales,

The following code will delete all blank rows (except for those with vertically merged cells) in all tables in the document:

Sub DelBlankRows()
Dim Pwd As String
Dim oRow As Integer
Dim oTable As Table
With ActiveDocument
If .Tables.Count > 0 Then
For Each oTable In .Tables
For oRow = oTable.Rows.Count To 1 Step -1
If Len(Replace(oTable.Rows(oRow).Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then
On Error Resume Next 'skip vertically merged cells
oTable.Rows(oRow).Delete
End If
Next oRow
Next oTable
End If
End With
End Sub

Cheers
 
M

macropod

You can delete 'Dim Pwd As String' - it was left over from another project.

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

macropod said:
Hi Morales,

The following code will delete all blank rows (except for those with vertically merged cells) in all tables in the document:

Sub DelBlankRows()
Dim Pwd As String
Dim oRow As Integer
Dim oTable As Table
With ActiveDocument
If .Tables.Count > 0 Then
For Each oTable In .Tables
For oRow = oTable.Rows.Count To 1 Step -1
If Len(Replace(oTable.Rows(oRow).Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then
On Error Resume Next 'skip vertically merged cells
oTable.Rows(oRow).Delete
End If
Next oRow
Next oTable
End If
End With
End Sub

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Morales said:
Hi All, I have already posted this message once, but I can't see it.
If it pops up later - apologies.
Can any of you gurus tell me how to select and delete a row of cells
using VBa if the row is empty. In most cases the tables will be one
cell deep by two cells wide. Thanks in advance.
 
G

Greg Maxey

Something like this:

Sub ScratchMacro()
Dim oTbl As Word.Table
Dim bEmpty As Boolean
Dim oRow As Word.Row
Dim oCell As Word.Cell
For Each oTbl In ActiveDocument.Range.Tables
For Each oRow In oTbl.Rows
bEmpty = True
For Each oCell In oRow.Cells
If Len(oCell.Range.Text) > 2 Then bEmpty = False
Next oCell
If bEmpty Then oRow.Delete
Next oRow
Next oTbl
End Sub
 
Joined
Sep 10, 2012
Messages
1
Reaction score
0
In my word document I have tables in which cells are vertically merged. Could you please let me know how to delete empty rows of such tables?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top