Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old Yesterday, 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: 5
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 Yesterday, 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,618
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 Yesterday, 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; Yesterday at 03:11 PM. Reason: Modified to use Table.Cell(r,c) as OP requested
Reply With Quote
  #4  
Old Yesterday, 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,483
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 Today, 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: 5
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 Today, 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,483
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 Today, 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: 303
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
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 06:32 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