Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-21-2025, 04:29 AM
BillH45 BillH45 is offline How to count the number of lines in a Word table cell Windows 11 How to count the number of lines in a Word table cell Office 2021
Novice
How to count the number of lines in a Word table cell
 
Join Date: Oct 2025
Location: Edinburgh, Scotland
Posts: 6
BillH45 is on a distinguished road
Default How to count the number of lines in a Word table cell

The title says it all, really.

I hope some helpful expert can come up with some code to determine the number of lines of text there are in a given table cell, referenced by tbl.Cell(r, c) where tbl is the table.
Reply With Quote
  #2  
Old 10-21-2025, 06:04 AM
gmaxey gmaxey is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,621
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

A bit cumbersome but may work for you:

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim strText As String
Dim lngIndex As Long
Dim oRng As Range
Dim oCol As New Collection
Dim strPosit As String
  strText = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
  For lngIndex = 1 To Len(strText)
    Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range.Characters(lngIndex)
    On Error Resume Next
    strPosit = CStr(oRng.Information(wdVerticalPositionRelativeToPage))
    oCol.Add strPosit, strPosit
   Next
  MsgBox oCol.Count
lbl_Exit:
  Exit Sub
End Sub

... of course all of the text will have to be on the same page.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 10-21-2025, 03:08 PM
Guessed's Avatar
Guessed Guessed is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,178
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Another non-optimal method is to use the selection object. It has the benefit of working across page breaks.
Code:
Sub HowManyLines()
  Dim aRng As Range, i As Integer, aTbl As Table
  Set aTbl = ActiveDocument.Tables(1)
  Set aRng = aTbl.Cell(1, 2).Range
  aRng.Select
  Selection.Collapse Direction:=wdCollapseStart
  Do While Selection.Range.End < aRng.End
    i = i + 1
    Selection.MoveDown Unit:=wdLine, Count:=1
  Loop
  MsgBox "Cell line count: " & i
End Sub
The problem with this method is that it relies on moving the selection. I wasn't able to find a Range.MoveDown option but if one existed then it would be better than using Selection.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 10-21-2025 at 03:11 PM. Reason: Modified to use Table.Cell(r,c) as OP requested
Reply With Quote
  #4  
Old 10-21-2025, 10:21 PM
macropod's Avatar
macropod macropod is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,489
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Similarly:
Code:
Dim p As Single, l As Long, Rng As Range
Set Rng = Tbl.Cell(r, c).Range
With Rng
  .End = .End - 1
  p = .Characters.First.Information(wdVerticalPositionRelativeToPage): l = 1
  Do While .Words.First.Start < .Words.Last.Start
    .MoveStart wdWord, 1
    If .Characters.First.Information(wdVerticalPositionRelativeToPage) > p Then
      p = .Characters.First.Information(wdVerticalPositionRelativeToPage): l = l + 1
    End If
  Loop
End With
MsgBox l
This, too, works when the cell spans a page break.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 10-22-2025, 03:57 AM
BillH45 BillH45 is offline How to count the number of lines in a Word table cell Windows 11 How to count the number of lines in a Word table cell Office 2021
Novice
How to count the number of lines in a Word table cell
 
Join Date: Oct 2025
Location: Edinburgh, Scotland
Posts: 6
BillH45 is on a distinguished road
Default

Thanks for the ideas. Maybe I should have mentioned that no cells involved will ever span a page so that's not an issue.

However, I have to report that only the code supplied by Guessed gives the right answer (at least in my environment) . The other two always come up with 1 line no matter how many there are (there will be from 1 to 5 in practice).
Reply With Quote
  #6  
Old 10-22-2025, 04:00 AM
macropod's Avatar
macropod macropod is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,489
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

In my testing, the code I posted easily counts to more than 50 lines, including when page breaks are spanned.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 10-22-2025, 05:12 AM
vivka vivka is offline How to count the number of lines in a Word table cell Windows 7 64bit How to count the number of lines in a Word table cell Office 2016
Expert
 
Join Date: Jul 2023
Posts: 304
vivka is on a distinguished road
Default

Hi, BillH45 ! If you replace the typo "<p" with ">p" in Paul's code, everything will work like a charm. All of the proposed methods are real gems: simple and nice to learn from (my opinion)!
Reply With Quote
  #8  
Old 10-22-2025, 08:40 AM
gmaxey gmaxey is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,621
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Paul, I tested with your code as well and was not getting proper results. Changing your


If .Characters.First.Information(wdVerticalPositionRe lativeToPage) < p Then To
If .Characters.First.Information(wdVerticalPositionRe lativeToPage) <> p Then

Resolved
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 10-22-2025, 01:36 PM
macropod's Avatar
macropod macropod is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,489
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmaxey View Post
Paul, I tested with your code as well and was not getting proper results. Changing your


If .Characters.First.Information(wdVerticalPositionRe lativeToPage) < p Then To
If .Characters.First.Information(wdVerticalPositionRe lativeToPage) <> p Then

Resolved
Actually,
Code:
If .Characters.First.Information(wdVerticalPositionRelativeToPage) <> p Then
is what I originally posted. But I realised I didn't need that and that
Code:
If .Characters.First.Information(wdVerticalPositionRelativeToPage) > p
would be sufficient. I then edited out the wrong character.

Corrected.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 10-22-2025, 08:57 AM
BillH45 BillH45 is offline How to count the number of lines in a Word table cell Windows 11 How to count the number of lines in a Word table cell Office 2021
Novice
How to count the number of lines in a Word table cell
 
Join Date: Oct 2025
Location: Edinburgh, Scotland
Posts: 6
BillH45 is on a distinguished road
Default

Thanks to all contributors. All versions are now working for me.

My expertise is in Excel VBA and Word macros are another country. With apologies to vivka, I have little idea of how these macros actually work, so can anyone advise on which is the most efficient?
Reply With Quote
  #11  
Old 10-22-2025, 09:39 AM
gmaxey gmaxey is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,621
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Quote:
Originally Posted by BillH45 View Post
Thanks to all contributors. All versions are now working for me.

My expertise is in Excel VBA and Word macros are another country. With apologies to vivka, I have little idea of how these macros actually work, so can anyone advise on which is the most efficient?

I would suggests using any working versions of Paul's (Macropod)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old Yesterday, 05:48 AM
gmaxey gmaxey is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,621
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Paul, I have tested and rested with a simple document with one 2 x 2 table where the text in the target cell splits across a page. In every instance without using "<> p" I do not get the expected count.


In one test there were two lines of text on the first page and 3 lines on the second page using either "< p" or "> P" the return is 2. Only with using "<> p" do I get the expected return of 5.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #13  
Old Yesterday, 01:01 PM
macropod's Avatar
macropod macropod is offline How to count the number of lines in a Word table cell Windows 10 How to count the number of lines in a Word table cell Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,489
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmaxey View Post
Paul, I have tested and rested with a simple document with one 2 x 2 table where the text in the target cell splits across a page. In every instance without using "<> p" I do not get the expected count.


In one test there were two lines of text on the first page and 3 lines on the second page using either "< p" or "> P" the return is 2. Only with using "<> p" do I get the expected return of 5.
Curious. For me the code works just fine with:
Code:
If .Characters.First.Information(wdVerticalPositionRelativeToPage) > p
returning 51 on a 2*2 table spanning two pages.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old Yesterday, 09:02 AM
vivka vivka is offline How to count the number of lines in a Word table cell Windows 7 64bit How to count the number of lines in a Word table cell Office 2016
Expert
 
Join Date: Jul 2023
Posts: 304
vivka is on a distinguished road
Default

BillH45, I think you can use any code you like because all of them work. They differ only in approaches to the task. I'll keep all of them in my collection (with proper references to the authors).
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write a macro to count the number of lines between the start of a Word document to where the soroush.kalantari Word VBA 3 08-08-2021 09:23 PM
How to count the number of lines in a Word table cell count the number of text in a cell based off a different cells text Kubi Excel 4 08-24-2017 05:53 PM
How to count the number of lines in a Word table cell Row Number of Selected Cell in Table alexolcz Word VBA 2 12-10-2015 09:54 PM
How can I delete spaces & lines in a table cell mrayncrental Word VBA 3 10-20-2014 07:09 PM
Text Wrapping on Fixed Lines in a Form field/Table cell okrmjr Word Tables 0 10-30-2009 08:52 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:35 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft