vba Deleting specific rows and columns in a word table

ES87ME

I need to parse most of a word doc tables and delete the cells containing the string "Deleted". The tables have different formats so i need to change the start index depending on the format.i wrote a code where i start first deleting rows with all cells containing the "Deleted" string. The same strategy is used to delete the columns. It worked for the rows but not for the columns delete. When running through the table cells the column index is bigger than the actual available columns due to deleted columns.i dont know why it occured in the column delete part and not in the row delete one. The code below works for some of the tables but not all of them.

Dim msWord                          As Word.Application
Dim myDoc                           As Word.Document
Dim wordTable                       As Table
Dim r                               As Long
Dim c                               As Long
Dim col_del_cnt                     As Long
Dim row_del_cnt                     As Long
Dim i                               As Long
Dim col_index                       As Long
Dim param                           As Long
Dim Tbl                             As Table
Dim Tmpfile                         As String

Tmpfile = ThisWorkbook.Path & "\Template.docx"
    With msWord
      .Visible = True
      Set myDoc = .Documents.Open(Filename:=Tmpfile)
    End With

    With myDoc
      For i = 7 To 21
      Set wordTable = .Tables(i)
      If wordTable.Columns.Count < 2 Then
        col_index = 1
        param = wordTable.Columns.Count
      Else
        col_index = 2
        param = wordTable.Columns.Count - 1
      End If

      For r = 2 To wordTable.Rows.Count
        col_del_cnt = 0
        For c = col_index To wordTable.Columns.Count
          If InStr(1, wordTable.Cell(r, c).Range.Text, "Deleted", 1) Then
            col_del_cnt = col_del_cnt + 1
          End If
        Next c

        If col_del_cnt = param Then
          If r > wordTable.Rows.Count Then
            wordTable.Rows(wordTable.Rows.Count).Delete
          Else
            wordTable.Rows(r).Delete
          End If
            End If
      Next r
    Next
  End With

  With myDoc
       For i = 7 To 21
    Set wordTable = .Tables(i)
    If wordTable.Columns.Count < 2 Then
      col_index = 1
    Else
      col_index = 2
    End If

    For c = col_index To wordTable.Columns.Count
      row_del_cnt = 0
      For r = 2 To wordTable.Rows.Count
                  If InStr(1, wordTable.Cell(r, c).Range.Text, "Deleted", 1) Then '\Error located here'
        row_del_cnt = row_del_cnt + 1
          End If
      Next r

      If row_del_cnt = wordTable.Rows.Count - 1 Then
        If c > wordTable.Columns.Count Then
        wordTable.Columns(wordTable.Columns.Count).Delete
        Else
        wordTable.Columns(c).Delete
        End If
      End If
            Next c
          Next
      End With

I hope someone could help me to find the solution.

macropod

It appears you're trying to delete both the row and the column when a cell has 'Deleted' in it. Obviously, if you use one loop to delete a row that has 'Deleted' in it, then a second loop to delete a column that has 'Deleted' in it, the second loop won't find anything. Try something based on:

Dim t As Long, r As Long, c As Long, ArrCols() As String
With myDoc
  For t = 21 To 7 Step -1
    With .Tables(t)
      If InStr(1, .Range.Text, "Deleted", 1) Then
        ReDim ArrCols(.Columns.Count)
        For r = .Rows.Count To 1 Step -1
          With .Rows(r)
            If InStr(1, .Range.Text, "Deleted", 1) Then
              For c = 1 To .Cells.Count
                If InStr(1, .Cells(c).Range.Text, "Deleted", 1) Then
                  ArrCols(c) = c
                End If
              Next
              .Delete
            End If
          End With
        Next r
        For c = UBound(ArrCols) To 1 Step -1
          If ArrCols(c) <> "" Then .Columns(c).Delete
        Next
      End If
    End With
  Next
End With

Note how all the loops involving deletions run backwards.

The fact your own code didn't throw errors with the row deletions was just a coincidence.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleting multiple rows from a table

From Dev

VBA code for deleting a word

From Dev

Iterate over all rows and columns in table with VBA

From Dev

Deleting a specific rows and columns from a data-set using Matlab

From Dev

Summing across rows of a data.table for specific columns

From Dev

Dynamically added table rows not deleting

From Dev

VBA Word table with unknown number of fused rows/columns

From Dev

changing color and font of specific cells in word table with vba

From Dev

Deleting an Array of rows and columns

From Dev

Deleting rows with duplicate info in columns

From Dev

Deleting Blank Pages in Word Doc with VBA

From Dev

Excel VBA - Add new table columns with specific header names

From Dev

Deleting a specific word form a text file in python

From Dev

R: Conditional deleting rows and columns in a matrix

From Dev

Import specific rows and columns form csv file into table using php

From Dev

Deleting multiple rows from a table

From Dev

deleting rows containing excel vba

From Dev

Deleting specific rows/columns from excel

From Dev

Find rows in SQL table that have specific part of value columns

From Dev

Deleting CommandButtons by name in VBA Word 2010

From Dev

Deleting specific columns based on conditions

From Dev

VBA Word table with unknown number of fused rows/columns

From Dev

Insert Rows in Word table

From Dev

Removing rows in a R data.table with NAs in specific columns

From Dev

Deleting unwanted columns starting at a specific row - excel vba

From Dev

issue with VBA Copy specific row/column from Word table (with merged rows) to Excel

From Dev

Deleting rows in HTML table

From Dev

Ensure Replace Text function only looks at a specific Word table VBA

From Dev

Insert specific rows from one table in database into another with different columns

Related Related

  1. 1

    Deleting multiple rows from a table

  2. 2

    VBA code for deleting a word

  3. 3

    Iterate over all rows and columns in table with VBA

  4. 4

    Deleting a specific rows and columns from a data-set using Matlab

  5. 5

    Summing across rows of a data.table for specific columns

  6. 6

    Dynamically added table rows not deleting

  7. 7

    VBA Word table with unknown number of fused rows/columns

  8. 8

    changing color and font of specific cells in word table with vba

  9. 9

    Deleting an Array of rows and columns

  10. 10

    Deleting rows with duplicate info in columns

  11. 11

    Deleting Blank Pages in Word Doc with VBA

  12. 12

    Excel VBA - Add new table columns with specific header names

  13. 13

    Deleting a specific word form a text file in python

  14. 14

    R: Conditional deleting rows and columns in a matrix

  15. 15

    Import specific rows and columns form csv file into table using php

  16. 16

    Deleting multiple rows from a table

  17. 17

    deleting rows containing excel vba

  18. 18

    Deleting specific rows/columns from excel

  19. 19

    Find rows in SQL table that have specific part of value columns

  20. 20

    Deleting CommandButtons by name in VBA Word 2010

  21. 21

    Deleting specific columns based on conditions

  22. 22

    VBA Word table with unknown number of fused rows/columns

  23. 23

    Insert Rows in Word table

  24. 24

    Removing rows in a R data.table with NAs in specific columns

  25. 25

    Deleting unwanted columns starting at a specific row - excel vba

  26. 26

    issue with VBA Copy specific row/column from Word table (with merged rows) to Excel

  27. 27

    Deleting rows in HTML table

  28. 28

    Ensure Replace Text function only looks at a specific Word table VBA

  29. 29

    Insert specific rows from one table in database into another with different columns

HotTag

Archive