/VBA Tips /How to Count Number of Rows in a Word Table?
Microsoft Word does not have an automatic feature to count the number of rows in a table; however, you can create a simple macro to return these values
Method 1: Use the Information property to return the number of rows
Use the Microsoft Visual Basic for Applications Selection.Information property to return the number of rows in a table.
NOTE: The insertion point must be located in the table before you run the macro.
Sub numRowsInCurrentTable() MsgBox Selection.Information(wdMaximumNumberOfRows) End sub
Method 2: Use the the Tables property to return the number of rows
Use the Microsoft Visual Basic for Applications Tables property to return the number of rows within a table. This method will return the total number of rows for the first table in the document. The insertion point does not need to be in the table.
Sub numRowsInSpecificTable() MsgBox ActiveDocument.Tables(1).Rows.Count End Sub
Quick Steps
- Open MS Word and Select any table
- Press Alt+F11, Copy Paste the code in this Article
- Press F5.
Related
Permalink
Cannot retrieve contributors at this time
title | keywords | f1_keywords | ms.prod | api_name | ms.assetid | ms.date | ms.localizationpriority |
---|---|---|---|---|---|---|---|
Rows.Count property (Word) |
vbawd10.chm155975682 |
vbawd10.chm155975682 |
word |
Word.Rows.Count |
6e326ef4-2a5e-dd90-a1bb-c2b6d59006e6 |
06/08/2017 |
medium |
Rows.Count property (Word)
Returns a Long that represents the number of rows in the collection. Read-only.
Syntax
expression.Count
expression Required. A variable that represents a Rows object.
See also
Rows Collection Object
[!includeSupport and feedback]
Если ваш документ содержит таблицу и вам нужно подсчитать количество строк в ней, то результат можно получить двумя способами.
Первый способ заключается в том, что вы выделяете таблицу, открываете диалоговое окно Свойства таблицы и на вкладке Строка узнаете количество строк:
Второй способ заключается в написании простейшего макроса, который подсчитывает количество строк в текущей таблице (в которой находится курсор ввода) и выводит окошко с информацией:
Откройте редактор Visual Basic (Alt+F11) и вставьте в какой-либо модуль шаблона Normal.dot следующий код:
Sub rowsCount() ' Подсчет количества строк в текущей таблице Dim n As Integer n = Selection.Tables(1).Rows.Count MsgBox prompt:="В таблице " & n & " строк", _ Title:="Подсчет строк в таблице" End Sub
Чтобы иметь быстрый доступ к этому макросу, назначьте для него сочетание клавиш или создайте кнопку на панели инструментов или в панели быстрого доступа (Word 2007).
I opened a Word Doc,which had tables in it, use VBA from access. I want to add a new line and write something in that line. I worte some code but that code worked only in debug mode. As soon as i clear all break point and let the programm run, i will have a problem. It seems that word will only write data into the first line. Please help。
for j = 0 to 5
oDoc.tables(1).rows.Add
for k=0 to 5
oDoc.tables(1).cell(oDoc.tables(1).rows.count,k+1)=myarray(k)
next k
do something to myarray
next j
Deduplicator
44.3k7 gold badges65 silver badges115 bronze badges
asked Oct 15, 2010 at 3:44
2
This is a little strange — I think the code will only work if your myarray() is an array of Range objects, which seems a bit weird. If I make a 3×1 table in my current document and run the following code:
Sub x()
For j = 1 To 5
ActiveDocument.Tables(1).Rows.Add
For k = 0 To 2
ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count, k + 1).Range.Text = "hello"
Next k
Next j
End Sub
… then it seems to work fine in both debug and normal runs. This is in Office 2010.
My guess is that you have some strangeness going on with the myarray() — if it’s not actually an array of Range objects, then try explicitly setting .Range.Text as I did in my code and I bet it’ll be happier.
Chris
answered Jan 12, 2011 at 19:58
Chris RaeChris Rae
5,6272 gold badges36 silver badges51 bronze badges
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS
Contact US
Thanks. We have received your request and will respond promptly.
Log In
Come Join Us!
Are you a
Computer / IT professional?
Join Tek-Tips Forums!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts - Keyword Search
- One-Click Access To Your
Favorite Forums - Automated Signatures
On Your Posts - Best Of All, It’s Free!
*Tek-Tips’s functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Posting Guidelines
Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Students Click Here
can’t count rows in microsoft wordcan’t count rows in microsoft word(OP) 4 May 05 22:18 how do i count the number of rows in a selection (not table, but highlited area) for microsoft word. I can only seem to get it to work for tables. Red Flag SubmittedThank you for helping keep Tek-Tips Forums free from inappropriate posts. |
Join Tek-Tips® Today!
Join your peers on the Internet’s largest technical computer professional community.
It’s easy to join and it’s free.
Here’s Why Members Love Tek-Tips Forums:
- Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More…
Register now while it’s still free!
Already a member? Close this window and log in.
Join Us Close