Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save s-hiiragi/b041324671bf5fd5c6111d9a39d6d1e1 to your computer and use it in GitHub Desktop.
Save s-hiiragi/b041324671bf5fd5c6111d9a39d6d1e1 to your computer and use it in GitHub Desktop.
Iterate through a table containing merged cells
Sub IterateThroughTableContainingMergedCells()
Dim t As Table
Dim i As Long
Dim j As Long
For i = 1 To ActiveDocument.Tables.Count
Set t = ActiveDocument.Tables(i)
Debug.Print "Table(" & i & "): rows=" & t.Rows.Count & ", columns=" & t.Columns.Count
For j = 1 To t.Rows.Count
'Debug.Print t.Rows(j).Cells.Count
' ==> 以下のエラーが発生する
' ---
' 実行時エラー '5991':
' 表内に縦方向に結合されたセルが存在するため、このコレクション内の一部の行にアクセスできません。
Next
' Selection.Cellsを使うことでエラーを回避できる
t.Select
With Selection
Debug.Print .Cells.Count
For j = 1 To .Cells.Count
Debug.Print "Cell(" & j & "): (" & .Cells(j).RowIndex & ", " & .Cells(j).ColumnIndex & ")"
Next
End With
Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment