2
votes

In a Word 2010 document with 30+ tables, some with text only and the rest financial tables. I need to replace all the financial tables with a [TABLE] placeholder. How do I use VBA to select the financial tables only? I figure I could search for numbers (integers) in two adjacent cells, select the table, delete it and replace it with a text placeholder [TABLE] and go on to find the next financial table? Please help.

1

1 Answers

4
votes

Something like this:

Sub ReplaceTables()
    Dim oTable As Table
    Dim oRng As Range
    For Each oTable In ThisDocument.Tables
        If oTable.Rows.Count > 1 And oTable.Columns.Count > 1 Then
            If IsNumeric(oTable.Cell(2, 1).Range.Words(1).Text) And _
                        IsNumeric(oTable.Cell(2, 2).Range.Words(1).Text) Then
                Set oRng = oTable.Range
                oTable.Delete
                oRng.Text = "[TABLE]" & vbCrLf
            End If
        End If
    Next
    Set oTable = Nothing
    Set oRng = Nothing
End Sub

It loops through all the tables in the document, checks in the first two cells in the second row if the first word is numeric, and if so deletes the table and puts the [TABLE] text and a new line instead.

Hope this helps.

Addition:

To check for 4 columns or more, and the presence of a $ sign in the text of the table, use this check instead:

If oTable.Columns.Count >= 4 Then
    If InStrRev(oTable.Range.Text, "$") > 0 Then