how can I count the number of rows in an existing table

R

roberto-cruz

i've got a table, about 45 rows, but I want a way to count the exact number
of rows without adding a separate column on the left and numbering it from 1
to 45 or whatever. because i don't want that column to appear in the table
when I print it or display it, I just want a line a the bottom of the doc
that says: " there are "n" rows in your table.
 
S

Suzanne S. Barnhill

L

Lene Fredborg

You could use the following solution:

Preconditions: Your table must have at least 2 columns (otherwise, the
explanation below will need adjustments).

1. At the end of your table, add a row to be used for the count info.
2. In the first cell in the info row, insert a MacroButton field as follows:

3. Press Ctrl+F9 to insert a set of field braces.
4. Between the braces, type:

MacroButton CountTableRows Double-click here to count rows

5. Press F9 to update the field (if this does not toggle field codes, press
Alt+F9).
6. To make it easy to distinguish the field from the rest of the content,
you may e.g. add yellow highlight to it.

Note that "Double-click here to count rows" is the text that will appear in
the field. You can change the text as you wish.

Now you need a macro named "CountTableRows". The macro will run whenever a
user double-clicks the MacroButton field. Such macro is found below (comments
are included in the macro to tell what happens) - it inserts the text "There
are [n] rows in your table (incl. this row)" in the last table cell (it
replaces the current cell content and you don't need to type any of the text
manually).

Sub CountTableRows()
Dim n As Long
Dim oRange As Range

'Since last operation was a click on the MacroButton field,
'the selection is in the table
With Selection
'Go to the last cell
'Done as follows to prevent errors in case of merged cells
.Start = .Tables(1).Range.Characters.Last.Start - 1
'Collapse selection
.End = .Start
'The last cell must be used as range later
Set oRange = .Cells(1).Range
'Find max number of rows in table
n = Selection.Information(wdMaximumNumberOfRows)
'Replace content of last cell by updated info
oRange.Text = "There are " & n & " rows in your table (incl. this
row)"
End With
'Clean up
Set oRange = Nothing
End Sub

The macro must be stored in a place that makes it available to all users.
This can be:
- in the document itself (i.e. the document with the table)
- in the attached template, provided the template is available to all users
(not your Normal.dot)
- in a global template, i.e. a template stored in Word's Startup folder

For more information about MacroButton fields, see:
http://www.word.mvps.org/FAQs/TblsFldsFms/UsingMacroButton.htm

For information about how to install a macro, see:
http://www.gmayor.com/installing_macro.htm

NOTE: if the users need to add rows to the end of the table, it may be
better to place the info row at the top of the table or somewhere else. In
that case, adjustments to the macro are needed. You only need to be able to
uniquely identify the table in which to count rows and the place to put the
updated count.

The user must double-click the MacroButton field to update the rows count.
It is not updated automatically.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top