title: Excel VBA Import Word table with merged cells to Excel
body: <p>I have many tables in a Word document (.docx) and I want to import them to a blank Excel sheet in an easy way. The tables in the Word document are not the same size (rows) and some rows have merged cells.</p>
  
  <p>My code is below. I can choose the .docx and then select the number of the table to import but I only can import the headers, so I do not know if works fine. I am doing this because I want to keep the tables format (same rows) and is not valid if I use copy/paste.</p>
  
  <p>When I run this code I get an error:</p>
  
  <blockquote>
    <p>Run-time error '5941'. The requested member of the collection does not exist.</p>
  </blockquote>
  
  <p>On this line:</p>
  
  <pre><code>Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
  </code></pre>
  
  <p>This is the code:</p>
  
  <pre><code>Sub ImportWordTable()
  Dim wdDoc As Object
  Dim wdFileName As Variant
  Dim TableNo As Integer 'table number in Word
  Dim iRow As Long 'row index in Excel
  Dim iCol As Integer 'column index in Excel
  
  wdFileName = Application.GetOpenFilename("Word files (*.docx),*.doc", , _
  "Browse for file containing table to be imported")
  
  If wdFileName = False Then Exit Sub '(user cancelled import file browser)
  
  Set wdDoc = GetObject(wdFileName) 'open Word file
  
  With wdDoc
      TableNo = wdDoc.tables.Count
      If TableNo = 0 Then
          MsgBox "This document contains no tables", _
          vbExclamation, "Import Word Table"
      ElseIf TableNo &gt; 1 Then
          TableNo = InputBox("This Word document contains " &amp; TableNo &amp; " tables." &amp; vbCrLf &amp; _
          "Enter table number of table to import", "Import Word Table", "1")
      End If
      With .tables(TableNo)
          'copy cell contents from Word table cells to Excel cells
          For iRow = 1 To .Rows.Count
              For iCol = 1 To .Columns.Count
                  Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
              Next iCol
          Next iRow
      End With
  End With
  
  Set wdDoc = Nothing
  
  End Sub
  </code></pre>
  
  <p>The format of my tables is the following:</p>
  
  <pre><code>&lt;header&gt; Same number of rows for all
  6 rows with 2 columns
  &lt;/header&gt;
  &lt;content of the table&gt;
  &lt;header1&gt;3 columns combined&lt;header1&gt;
  multiple rows with 3 columns
  &lt;header1&gt;3 columns combined&lt;header1&gt;
  multiple rows with 3 columns
  &lt;/content of the table&gt;
  </code></pre>
  
  <p>Is something like this:</p>
  
  <pre><code>_______________________
  |_________|____________|
  |_________|____________|
  |_________|____________|
  |_________|____________|
  |_________|____________|
  |_________|____________|
  |______________________|
  |_____|__________|_____|
  |_____|__________|_____|
  |_____|__________|_____|
  |_____|__________|_____|
  |_____|__________|_____|
  |______________________|
  |_____|__________|_____|
  |_____|__________|_____|
  |_____|__________|_____|
  |_____|__________|_____|
  |_____|__________|_____|
  </code></pre>
  
  <p>Sorry for the table format but I do not know how to explain it better. The final goal is to leave it in excel as follows:</p>
  
  <pre><code>_______________________
  |_________|____________|
  |_________|____________|
  |_________|____________|
  |_________|____________|
  |_________|____________|
  |_________|____________|
  |______________________||______________________|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  |_____|__________|_____||_____|__________|_____|
  </code></pre>
  
  <p>How can I split the merged cells before insert in Excel? The steps would be to detect one by one as now the cells and when only found 1 split the cell or use as one</p>