2
votes

I have a number of MS Word tables with 11 cols that I need to edit.

Each document will contain 1 table.

I want to edit a table from 11 cols to 6 cols.

VBA to perform the following tasks:

  1. Rename col 2 heading from Time to Item
  2. Rename col 3 heading from Time Zone to Comment
  3. Rename col 5 heading from Type to E or A

  4. Delete all text in cols 2, 3, and 5 (not the renamed headings)

  5. Delete cols 6, 7, 9, 10, 11

I have listed the order of editing the table in what I believe will work best but it really does not matter.

I don't know if the text in cols 2, 3, 5 can be deleted without deleting the headings also.

Finally, I guess that I could just enter the new headings that I want in cols 2, 3, 5 if those headings are deleted when the text in those cols are deleted.

Thanks for any help.

1
Have you tried anything?Siddharth Rout

1 Answers

0
votes

Hope this helps,

Option Explicit

Private Sub myTableMacro()
    Dim tmpTable As Word.Table, tmpRange As Word.Range
    With ActiveDocument
        If .Tables.Count > 0 Then
            For Each tmpTable In .Tables
                    'Rename col 2 heading from Time to Item
                tmpTable.Cell(1, 2).Range.Text = "Item"
                    'Rename col 3 heading from Time Zone to Comment
                tmpTable.Cell(1, 3).Range.Text = "Comment"
                    'Rename col 5 heading from Type to E or A
                tmpTable.Cell(1, 5).Range.Text = "E or A"
                    'Delete all text in cols 2, 3 (but not the headers)
                Set tmpRange = .Range(tmpTable.Cell(2, 2).Range.Start, tmpTable.Cell(tmpTable.Rows.Count, 3).Range.End)
                tmpRange.Select
                Selection.Delete Unit:=wdCharacter, Count:=1
                    'Delete all text in cols 5 (but not the headers)
                Set tmpRange = .Range(tmpTable.Cell(2, 5).Range.Start, tmpTable.Cell(tmpTable.Rows.Count, 5).Range.End)
                tmpRange.Select
                Selection.Delete Unit:=wdCharacter, Count:=1
                    'Delete cols 10, 11
                Set tmpRange = .Range(tmpTable.Cell(1, 9).Range.Start, tmpTable.Cell(1, 11).Range.End)
                tmpRange.Select
                Selection.Cells.Delete wdDeleteCellsEntireColumn
                    'Delete cols 6, 7, 9
                Set tmpRange = .Range(tmpTable.Cell(1, 6).Range.Start, tmpTable.Cell(1, 7).Range.End)
                tmpRange.Select
                Selection.Cells.Delete wdDeleteCellsEntireColumn
            Next 'tmpTable
        End If
    End With
    Set tmpRange = Nothing
    Set tmpTable = Nothing
End Sub

I have tested and found OK.