Results 1 to 13 of 13
  1. #1
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Table Selected Row Count (2003)

    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!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Table Selected Row Count (2003)

    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

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Table Selected Row Count (2003)

    If you want to know how many rows are in the currently selected table then try:
    <code>
    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
    </code>

    StuartR

  4. #4
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Table Selected Row Count (2003)

    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.
    Attached Files Attached Files

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Table Selected Row Count (2003)

    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.

  6. #6
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Table Selected Row Count (2003)

    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

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Table Selected Row Count (2003)

    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.)

  8. #8
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Table Selected Row Count (2003)

    Did Word 2002 return correct values for Application.Selection.Information(wdEndOfRangeRowN umber) - Application.Selection.Information(wdStartOfRangeCo lumnNumber) + 1?

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Table Selected Row Count (2003)

    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.

  10. #10
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Table Selected Row Count (2003)

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

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Table Selected Row Count (2003)

    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.

  12. #12
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Table Selected Row Count (2003)

    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.

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Table Selected Row Count (2003)

    > and the third row on top of the second page

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •