• Table Selected Row Count (2003)

    Author
    Topic
    #454204

    How do I get the count of the number of selected rows in a Word table?

    For example in Word create a table with a number of rows. Click to the immediate left of the table to highlight a number of rows. In VBA use Selection.Rows.Count… it comes up with the number of rows in your table. Selection.Range.Rows.Count sometimes gives the correct number selected, but sometimes gives 1. Any ideas?

    Thanks!

    Viewing 1 reply thread
    Author
    Replies
    • #1126846

      Selection.Rows.Count returns the number of selected rows, not the number of rows in the table (unless you selected the entire table, of course). You could also look at

      Selection.Information(wdEndOfRangeRowNumber) – Selection.Information(wdStartOfRangeRowNumber) + 1

      • #1126854

        Thanks for your reply, and I have tried these. I have included a sample Test.doc. If you scan down you will see a table in 3. (actually several tables combined to look like one table). with two highlighted sections that have borders. When you click to the immediate left in the table to select a row (or rows) and run the macro GetSelectedRowsCount located in Module1 more than often it does not return the correct results, at least for me.

        • #1126858

          Selection.Rows.Count consistently returns the number of selected rows for me in Word 2002 SP3 if the selection is within a single table.

          If the selection spans rows from more than one table, Selection.Rows.Count returns 0 and Selection.Information(wdEndOfRangeRowNumber) – Selection.Information(wdStartOfRangeColumnNumber) + 1 returns an incorrect result. This is to be expected since Selection.Information(wdEndOfRangeRowNumber) refers to a different table than Selection.Information(wdStartOfRangeColumnNumber) so the numbers are not related.

          • #1126860

            My code originally used Selection.Rows.Count but was getting bad return values.

            As I have several test beds, opening the same Test.doc document on Word 2003 SP2 vs SP3 I get different results on Selection.Rows.Count. I suppose this is a patch, but it does compound things in that we require SP2 for our customers to load our documents.

            Thanks for your input.
            Don

            • #1126863

              As far as I know all PCs at the place where I work have been updated to Word 2003 SP3 so I can’t test on Word 2003 SP2. (My replies up to now are based on Word 2002, the version I have at home.)

            • #1126864

              Did Word 2002 return correct values for Application.Selection.Information(wdEndOfRangeRowNumber) – Application.Selection.Information(wdStartOfRangeColumnNumber) + 1?

            • #1126868

              If the selection is within a single table, Selection.Information(wdEndOfRangeRowNumber) – Selection.Information(wdStartOfRangeColumnNumber) + 1 returns the same (correct) number as Selection.Rows.Count.

              If the selection spans more than one table, the result of Selection.Information(wdEndOfRangeRowNumber) – Selection.Information(wdStartOfRangeColumnNumber) + 1 is meaningless.

            • #1126959

              Is there a way to spin through all tables in a document to count the number of rows selected in each?

            • #1126963

              No doubt it’s possible, but I would prefer to avoid joining different tables. Since it’s possible to format rows and columns in a table individually, there’s no need to join different tables. You can then simply use Selection.Rows.Count instead of jumping through hoops to get the count.

            • #1126973

              If you would not mind, using the test doc perform this test. Go to the bottom of page one after “COMPUTATION” and highlight a row in the table, the insert a new row. Then click on the first row on bottom of the first page, and the third row on top of the second page. Then run Module1.GetSelectedRowCount in VBA. The result in Word 2003 SP3 is 3. It should be two.

            • #1126975

              > and the third row on top of the second page

              How? Holding down Shift, or Ctrl, or dragging?

    • #1126851

      If you want to know how many rows are in the currently selected table then try:

      Sub RowsInTable()

      If Selection.Information(wdWithInTable) Then
      MsgBox "This table has " & Selection.Tables(1).Rows.Count & " rows.", vbOKOnly
      Else
      MsgBox "Selection is not in a table.", vbOKOnly
      End If

      End Sub

      StuartR

    Viewing 1 reply thread
    Reply To: Reply #1126863 in Table Selected Row Count (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel