Share via

Determine table statistics using VBA

Anonymous
2013-10-26T09:11:36+00:00

how to count the number of cells per row and per column, annd compare the width and height of cells in a MS Word table using VBA?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2013-10-26T15:56:41+00:00

    Further polishing the cannon ball forged by Doug:

    Dim i As Long, j As Long

    Dim strData As String, strHeight As String

     With Selection.Tables(1)

       If Selection.Tables(1).Uniform Then

         strData = "The selected table is uniform and contains " & .Rows.Count & " rows and " & .Columns.Count & " columns." & vbCr

         For i = 1 To .Rows.Count

           For j = 1 To .Columns.Count

             If .Cell(i, j).Height = "9999999" Then

               strHeight = "undefined"

             Else

               strHeight = "at least " & .Cell(i, j).Height & " high"

             End If

             strData = strData & "The dimensions of cell " & j & " in row " & i & " are " & strHeight & " x " & .Cell(i, j).Width & " wide" & vbCr

           Next j

         Next i

       Else

         strData = "The selected table is not uniform and contains " & .Rows.Count & " rows and " & .Columns.Count & " columns." & vbCr

         For i = 1 To .Rows.Count

           For j = 1 To .Columns.Count

             On Error Resume Next

             If .Cell(i, j).Height = "9999999" Then

               strHeight = "undefined"

             Else

               strHeight = "at least " & .Cell(i, j).Height & " high"

             End If

             strData = strData & "The dimensions of cell " & j & " in row " & i & " are " & strHeight & " x " & .Cell(i, j).Width & " wide" & vbCr

             On Error GoTo 0

           Next j

         Next i

       End If

     End With

     Debug.Print strData

    Was this answer helpful?

    0 comments No comments
  2. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2013-10-26T15:20:09+00:00

    Another caveat is that VBA has great difficulty dealing with tables that contain merged or split cells. Usually you'll get a runtime error message that says something like "The requested member of the collection does not exist." If this happens, you may be able to work around it by inserting the line

       On Error Resume Next

    between the "For j = 1" line and the following MsgBox line.

    Was this answer helpful?

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2013-10-26T10:19:53+00:00

    Dim i As Long, j As Long

    With Selection.Tables(1)

        MsgBox "The selected table contains " & .Rows.Count & " rows and " & .Columns.Count & " columns."

        For i = 1 To .Rows.Count

            For j = 1 To .Columns.Count

                MsgBox "The dimensions of cell " & j & " in row " & i & " are " & .Cell(i, j).Height & " x " & .Cell(i, j).Width & "."

            Next j

        Next i

    End With

    If the HeightRule property of the specified row is wdRowHeightAuto, Height returns wdUndefined (the result will be 9(the 999999)****; setting the Height property sets HeightRule to wdRowHeightAtLeast. Read/write Single.

    Was this answer helpful?

    0 comments No comments