Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Counting rows in Tables (W2K (Sr-1))

    Is there a way to count the rows in a Table without numbering them sequentially. I would like to be able to count rows and have a line at the bottom of the table, but not in the actual table itself, that says something like "Number of rows in table = N" where is is the number of rows in the table. This would could be updated somehow (automatically?) if the number of rows changed. Any insight that anyone can provide is greatly appreciated. Thanks.

    Ron M <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  2. #2
    New Lounger
    Join Date
    Nov 2002
    Location
    Belleville, Illinois, USA
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting rows in Tables (W2K (Sr-1))

    Hi Ron,
    <Is there a way to count the rows in a Table without numbering them sequentially.>

    Select the table, go to the table properties and select the "row" tab. there you will find the number of rows in your table. I imagine a macro could be written to capture the table properties information, however, that is above my level of expertise and hopefully someone else in the lounge can help.

    Gerry <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  3. #3
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting rows in Tables (W2K (Sr-1))

    Ron,

    This will get you the row count in the table:

    Dim tbl As Table

    For Each tbl In ActiveDocument.Tables
    Debug.Print tbl.Rows.Count
    Next

    You will then need to insert the value of the Rows.Count into your document.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  4. #4
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Counting rows in Tables (W2K (Sr-1))

    Thanks Gerry.

    Maybe I am missing something here. <img src=/S/confused.gif border=0 alt=confused width=15 height=20> If I do as you suggest, all that is displayed when I select the "Row" is the number of the row that the cursor is in, e.g., Row 2: I am afraid I cannot see anything that tells me the total number of rows, unless I position the cursor in the last row of the table. Is that what you were suggesting?

    Ron M

  5. #5
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Counting rows in Tables (W2K (Sr-1))

    Thanks Bryan. I appreciate your help. My next question is how do I insert the value of Rows.Count into my document? Do I need to setup a {field} (somehow) to receive the value?

    Ron M

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Counting rows in Tables (W2K (Sr-1))

    I think the easiest way would be a macro that:

    1. <LI>Selects the current table (the one your insertion point is in)
      Selection.Tables(1).Select
      <LI>Captures the number of rows
      intRowNum = Selection.Information(wdEndOfRangeRowNumber)
      <LI>Collapses to the end of the selection (insertion point immediately following the table)
      Selection.Collapse Direction:=wdCollapseEnd
      <LI>Type in the desired table "footer" and then a paragraph symbol
      Selection.TypeText "Table has " & intRowNum & " rows." & vbCrLf
    If the stylization of the table footer is important, you could insert an AutoText entry that has the settings desired, and then insert the number of rows into it.

    Does this sound like a reasonable work flow?

    Note: There might well be a great way to do this with fields, but I'm not too familiar with calculations in fields...

  7. #7
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Counting rows in Tables (W2K (Sr-1))

    Phil - thanks for the clarification. What I was missing was the selection of the entire table. <img src=/S/blush.gif border=0 alt=blush width=15 height=15> <img src=/S/doh.gif border=0 alt=doh width=15 height=15> That worked when I did that

    Ron

  8. #8
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Counting rows in Tables (W2K (Sr-1))

    Jscher - Thanks - looks like a reasonable workflow. When I said fields, I actually meant bookmarks. I know, for example, there is a way to sequentially number Word documents using a bookmark field (something you might want with invoices, or for use with document control). I was thinking that there is probably a way to do it so the macro updates the bookmark when it is run.

    Since I do not want to include the Table heading row, I assume that the statement No. 2 would read:

    intRowNum = Selection.Information(wdEndOfRangeRowNumber)-1

    Ron M <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  9. #9
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting rows in Tables (W2K (Sr-1))

    Hi Ron:
    The information that Gerry gave you is on the row tab where it says Rows 1-X, where X = the number of the last row. It's not on the status bar, if that's where you're looking. Make sure you select the table first (e.g. Alt+ double click). Hope this is clear.

  10. #10
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting rows in Tables (W2K (Sr-1))

    Ron,

    Here is one way to do it. I have had to change the original code I posted, but this should do it in conjunction with some DocVariable fields.

    Sub GetTableRows()
    Dim doc As Document
    Dim tbl As Table
    Dim strDVName As String
    Dim lngLoop As Long
    Dim lngCount As Long

    Set doc = ActiveDocument
    lngCount = doc.Tables.Count
    'Loop through all the tables
    For lngLoop = 1 To lngCount
    'Set the DocVariable to the number of rows in the table
    doc.Variables("Table" & CStr(lngLoop) & "Rows") = doc.Tables(lngLoop).Rows.Count
    Next
    End Sub

    And below each table you need to add a DocVariable field that looks like

    { DOCVARIABLE "Table1Rows" MERGEFORMAT )

    Where the number in the doc variable is the table number In the document.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  11. #11
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Counting rows in Tables (W2K (Sr-1))

    Thanks Bryan. <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> Since I only want it to work on tables that I select, I suspect that some combination of what jscher has suggested and what you have given me will work exactly the way I would like it to. When I have it all worked out, I will post the code that finally worked. Again - Thanks <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> to all who helped and I hope you all have a Happy New Year with best wishes for 2003.

    Ron M <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  12. #12
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting rows in Tables (W2K (Sr-1))

    Glad to help Ron.

    If you have any problems combining the two suggestions, you know where to find us <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  13. #13
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Counting rows in Tables (W2K (Sr-1))

    So this is what I have manage to put together from jscher and Bryan's input. It runs, no errors, but does not fill in the DOCVARABLE that is defined in the document. <img src=/S/help.gif border=0 alt=help width=23 height=15> Thanks

    Ron M <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>


    Sub TableRowCount()
    '
    ' Table_Row_Count Macro
    ' Macro recorded 2002/12/31 by Ron May
    ' Modified 2003/01/02 by Ron May
    ' Code from jscher - Woody's Lounge
    ' Some Code from Bryan Carbonnell - Woody's Lounge
    '
    ' Selection point must be in the Table
    '
    Dim doc As Document
    Dim intRowNumber As Integer
    '
    Set doc = ActiveDocument
    Selection.Tables(1).Select
    intRowNum = Selection.Information(wdEndOfRangeRowNumber) - 1
    doc.Variables("RowCount") = intRowNum
    End Sub

  14. #14
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting rows in Tables (W2K (Sr-1))

    Ron,

    3 things.

    1. You are dimming intRowNumber but using intRowNum
    2. Selection.Tables(1).Select should be doc.Tables(1).select
    3. You need insert a DocVariable field in the Word doc and then update the fields with doc.Fields.Update

    Although I get the wrong number of rows with this.

    If you use this line:

    intRowNum = doc.Tables(1).Rows.Count

    instead of:

    Selection.Tables(1).Select
    intRowNum = Selection.Information(wdEndOfRangeRowNumber) - 1

    I get the correct number of rows returned. Plus the added bonus is that you don't move your selection or insertion point.

    I am attaching a doc that shows what I mean.

    Just add or remove rows from the table and then click the button on the doc to run the code.
    Attached Files Attached Files
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  15. #15
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Counting rows in Tables (W2K (Sr-1))

    Thanks Bryan. <img src=/S/blush.gif border=0 alt=blush width=15 height=15> on the dim statement, shows how long it's been since I did any serious programming <img src=/S/smile.gif border=0 alt=smile width=15 height=15>. The reason you may not have gotten the correct number of rows is that I have a header row in my table that I do not want to count, so I subtract one from the actual row count. What I am doing is inventorying apps etc., that use a particular type of data and I want a count to be automatic (more or less) so I do not have to manually count each row in the table everytime I add something.

    Again, Thanks for all your help with this.

    Ron M <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Page 1 of 2 12 LastLast

Posting Permissions

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