Table Macro Not Working 100% Full Capacity

D

David Schenkler

Hello!
I am using the following Macro in order to Auto Size Tables, see if the user wants Traces or not (Traces is specific to the document I'm using), and it will delete all rows which have its second cell blank/empty. It also bypasses the Error 5991.

However, it does not delete every row that has its second cell blank. It deletes some, but not others. Can someone help me?

Thank you so much in advance for your help!

Private Sub AutoSizeTables()
Dim i As Long
With ActiveDocument
For i = 1 To .Tables.Count
.Tables(i).AutoFitBehavior wdAutoFitWindow
Next i
End With
With ActiveDocument
For i = 1 To .Tables.Count
.Tables(i).AutoFitBehavior wdAutoFitContent
Next i
End With
End Sub

Private Sub Traces()
Dim i As Long
Dim Response As Long
Dim cell1 As Range
Response = MsgBox("Do you want Traces to be present in your report?", vbYesNo + vbDefaultButton1, "Traces")
With ActiveDocument
For i = .Tables.Count To 1 Step -1
Set cell1 = .Tables(i).cell(1, 1).Range
cell1.End = cell1.End - 1
If cell1 = "Traces From:" Then
If Response = vbYes Then
Selection.HomeKey Unit:=wdStory
ElseIf Response = vbNo Then
cell1.Tables(1).Delete
End If
End If
Next i
End With
End Sub

Private Sub DeleteCells()
' The following deletes any rows that have its second cell empty.
' Deletes most of them, but not all.
' It will keep one traec box even if it is blank if the user selected he/she wanted traces.
Dim oTbl As Table
Dim oRow As Row
Dim k As Long
Dim j As Long
For Each oTbl In ActiveDocument.Tables
For k = 1 To oTbl.Rows.Count
On Error Resume Next
k = oTbl.Rows(k).Index
If Err.Number <> 5991 Then
With oTbl.Rows(k)
If Len(Trim(.Cells(2).Range.Text)) = 2 Then
oTbl.Rows(k).Delete
End If
End With
End If
Next k
Next oTbl
End Sub
 
S

Stefan Blom

If you don't get a reply here, try the programming forum at Answers
(http://answers.microsoft.com/en-us/office/forum/customize?tab=all) or one of
the MSDN forums (such as Word for Developers at
http://social.msdn.microsoft.com/Forums/en-US/worddev/threads), all of which
target programming and reach a wider group of people than this group.

--
Stefan Blom
Microsoft Word MVP




"David Schenkler" wrote in message

Hello!
I am using the following Macro in order to Auto Size Tables, see if the user
wants Traces or not (Traces is specific to the document I'm using), and it will
delete all rows which have its second cell blank/empty. It also bypasses the
Error 5991.

However, it does not delete every row that has its second cell blank. It deletes
some, but not others. Can someone help me?

Thank you so much in advance for your help!

Private Sub AutoSizeTables()
Dim i As Long
With ActiveDocument
For i = 1 To .Tables.Count
.Tables(i).AutoFitBehavior wdAutoFitWindow
Next i
End With
With ActiveDocument
For i = 1 To .Tables.Count
.Tables(i).AutoFitBehavior wdAutoFitContent
Next i
End With
End Sub

Private Sub Traces()
Dim i As Long
Dim Response As Long
Dim cell1 As Range
Response = MsgBox("Do you want Traces to be present in your report?",
vbYesNo + vbDefaultButton1, "Traces")
With ActiveDocument
For i = .Tables.Count To 1 Step -1
Set cell1 = .Tables(i).cell(1, 1).Range
cell1.End = cell1.End - 1
If cell1 = "Traces From:" Then
If Response = vbYes Then
Selection.HomeKey Unit:=wdStory
ElseIf Response = vbNo Then
cell1.Tables(1).Delete
End If
End If
Next i
End With
End Sub

Private Sub DeleteCells()
' The following deletes any rows that have its second cell empty.
' Deletes most of them, but not all.
' It will keep one traec box even if it is blank if the user selected he/she
wanted traces.
Dim oTbl As Table
Dim oRow As Row
Dim k As Long
Dim j As Long
For Each oTbl In ActiveDocument.Tables
For k = 1 To oTbl.Rows.Count
On Error Resume Next
k = oTbl.Rows(k).Index
If Err.Number <> 5991 Then
With oTbl.Rows(k)
If Len(Trim(.Cells(2).Range.Text)) = 2 Then
oTbl.Rows(k).Delete
End If
End With
End If
Next k
Next oTbl
End Sub
 

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