Results 1 to 14 of 14

Thread: loop through tables in word and change color of cells

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Jan 2021
    Posts
    7
    Location

    loop through tables in word and change color of cells

    Hello forum,
    I would like to have suggestions on how to change color in tables using VBA. The word file has many tables but all have the same structure, only the contents change. I need a macro which changes the background color of the cells according to the contents. So for example, if the cell contains the text "I" then I want that cell colored in blue, if insted it contains "V" then it should be red and so on for 8 different classes. The following is the macro I modified (the original one was developed by someone else) but when I run it nothing happen, and I don't understand where I'm wrong.
    Thank you for any help!
    Daniel

    Sub colora()
    Dim MyTable As Table
    Dim MyRow As Row
    Dim MyCell As Cell
    For Each MyTable In ActiveDocument.Tables
    For Each MyRow In MyTable.Rows
      For Each MyCell In MyRow.Cells
        If MyCell.Range.Text = "I" Then
          MyCell.Shading.BackgroundPatternColor = wdColorBlue
         ElseIf MyCell.Range.Text = "V" Then
          MyCell.Shading.BackgroundPatternColor = wdColorRed
        End If
    Next MyCell
    Next MyRow
    Next MyTable
    Set MyCell = Nothing
    Set MyRow = Nothing
    Set MyTable = Nothing
    End Sub
    Please note the crossposting below with the same question but differently formulated
    https://groups.google.com/g/microsof.../c/YLWdSId_14k
    Last edited by Bob Phillips; 01-24-2021 at 12:18 PM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •