title: Removing the Contents from the Cell of a Word Table using Excel VBA
body: <p>I am currently working on a project and am looking for some assistance. To give you guys a layout of what is happening, I will run through the scenario step by step.</p>
  
  <p>1)  Currently I have a string array called “AnimalNamesToRemove” (For this example the array with contain the following words), that contains words that are used as bookmarks in a word document that I am looking to remove off a word table referenced below:</p>
  
  <p><strong>AnimalNamesToRemove</strong></p>
  
  <p>AnimalCat, AnimalDog, AnimalBird</p>
  
  <p>2)  In addition to the array, a table in a word document exists that has the name of the animal in column one, as well as some information about the animal (the only information that is of importance is the name of the animal):</p>
  
  <p><strong>Word Table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/yRi3N.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yRi3N.jpg" alt="enter image description here"></a></p>
  
  <p>3)  For this scenario, I have an excel table that I am looking to use to reference the words in the array and the word table names, as there are already bookmarks in the word document being used that hold the names existing in the array. To bring these together, a two column excel spreadsheet exists that has the name of the bookmark and the actual animal name (Column two is referenced using the range named “myRangeRef”):</p>
  
  <p><strong>Spreadsheet</strong></p>
  
  <p><a href="https://i.stack.imgur.com/p5UiT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p5UiT.jpg" alt="enter image description here"></a></p>
  
  <p>4)  What I am looking to do is that for every value in the array stated above, if that value (ex. “AnimalDog”) is found in the spreadsheet table (i.e. column two “Bookmark Reference”) then offset to the respective cell beside it in the first column (i.e. “Dog”) and create a new comma delimited string with those values, the same as “AnimalNamesToRemove” (i.e. Cat, Dog, Bird) and then turn it into a string array named “AnimalsToDelete”. Once the array is created, and all the values have been selected in the first column and made into an array based on the reference in column two, I want to go row by row in the word table and for every value existing in the new array “AnimalsToDelete”, if that value (i.e. Cat, Dog, and Bird) exists in the word table (found in column one), I want the code to delete the entire row that the name is found in (see result shown below)</p>
  
  <p><strong>Example Result</strong></p>
  
  <p><a href="https://i.stack.imgur.com/54uDQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/54uDQ.jpg" alt="enter image description here"></a></p>
  
  <pre><code>Dim wdTable As Object
  Dim myRangeRef As Range
  Dim AnimalNamesToRemove As Variant
  Dim AnimalsToDelete As Variant
  Dim wdDoc As Object
  
  Set myRangeRef = ThisWorkbook.Sheets("Bookmark References").Range("B1:B6")
  
  Set wdTable = wdDoc.Tables(1)
  For i = LBound(AnimalNamesToRemove) To UBound(AnimalNamesToRemove)
      For Each cell In myRangeRef
          If InStr(1, cell.Value, AnimalNamesToRemove(i), vbTextCompare) Then
              aCell = cell.Offset(, -1).Value
              stTemp = stTemp &amp; "," &amp; aCell
          End If
      Next cell
  Next i
  
  stTemp = Mid(stTemp, 2)
  If Not Len(Trim(stTemp)) = 0 Then
      AnimalsToDelete = Split(stTemp, ",")
      For i = LBound(AnimalsToDelete) To UBound(AnimalsToDelete)
          For j = wdTable.Rows.Count To 2 Step -1
              If wdTable.cell(j, 1).Range.Text = AnimalsToDelete(i) Then wdTable.Rows(j).Delete
          Next j
      Next i
  End If
  </code></pre>
  
  <p>If you have any solutions and/or suggestions please comment them down below.</p>
  
  <p><strong>NOTE:</strong> The first section of code works for creating the string array (i.e. from "set wdTable =" to "next i"), its the removal of information from the word table that I'm having the issues with.
  Best,</p>
  
  <p>Jack Henderson</p>