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

    Author
    Topic
    #381228

    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 smile smile smile

    Viewing 1 reply thread
    Author
    Replies
    • #641513

      Hi Ron,

      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 smile

      • #641521

        Thanks Gerry.

        Maybe I am missing something here. confused 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

        • #641531

          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.

          • #641546

            Phil – thanks for the clarification. What I was missing was the selection of the entire table. blush doh That worked when I did that

            Ron

    • #641518

      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.

      • #641522

        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

        • #641541

          I think the easiest way would be a macro that:

          1. Selects the current table (the one your insertion point is in)
            Selection.Tables(1).Select
          2. Captures the number of rows
            intRowNum = Selection.Information(wdEndOfRangeRowNumber)
          3. Collapses to the end of the selection (insertion point immediately following the table)
            Selection.Collapse Direction:=wdCollapseEnd
          4. 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…

          • #641548

            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 smile smile smile

        • #641622

          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.

          • #641648

            Thanks Bryan. thankyou 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 thankyou to all who helped and I hope you all have a Happy New Year with best wishes for 2003.

            Ron M smile smile smile

          • #641783

            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. help Thanks

            Ron M brickwall smile smile

            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

            • #641788

              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.

            • #641813

              Thanks Bryan. blush on the dim statement, shows how long it’s been since I did any serious programming smile. 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 smile smile smile

            • #641925

              In the options for the VB Editor, you can have it insert Option Explicit automatically in all new modules. (Tools > Options > Require Variable Declaration) You then will know immediately about variable name typos. Immediately when you try to compile/run the module, I should say.

              This DocVariable approach seems a bit limited in that it could change every time you run the macro, and then any {

    Viewing 1 reply thread
    Reply To: Counting rows in Tables (W2K (Sr-1))

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

    Your information: