Table Macro Not Working 100% Full Capacity

Discussion in 'Word Tables' started by David Schenkler, Aug 22, 2012.

  1. 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
     
    David Schenkler, Aug 22, 2012
    #1
    1. Advertisements

  2. David Schenkler

    Stefan Blom Guest

    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
    news:...

    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
     
    Stefan Blom, Aug 23, 2012
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dom fantauzzo

    spell checking not working ina table

    Dom fantauzzo, Aug 9, 2003, in forum: Word Tables
    Replies:
    2
    Views:
    213
    Dom Fantauzo
    Aug 9, 2003
  2. cmc
    Replies:
    5
    Views:
    103
    Suzanne S. Barnhill
    Oct 22, 2003
  3. Park It! NY
    Replies:
    1
    Views:
    128
    garfield-n-odie [MVP]
    Mar 27, 2006
  4. Stephen Lim
    Replies:
    8
    Views:
    120
    Stefan Blom
    Apr 18, 2006
  5. mz
    Replies:
    1
    Views:
    120
    Jezebel
    Sep 2, 2006
  6. Greg
    Replies:
    6
    Views:
    139
  7. LKB
    Replies:
    0
    Views:
    71
  8. TimvG
    Replies:
    0
    Views:
    177
    TimvG
    Dec 8, 2009
Loading...