David Schenkler
2012-08-22 17:38:17 UTC
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
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