Forum Discussion
vba table valid row count
a_bonsey Can you upload a copy of the document
Sure. Here you go
- May 17, 2022
a_bonsey That document contains a Word table, not an Excel worksheet.
The following macro will tell you how many rows contain data:
With ActiveDocument.Tables(1) For i = 1 To .Rows.Count If Len(.Cell(i, 1).Range) < 3 Then Exit For End If Next i End With MsgBox "The table contains data in " & i - 1 & " rows, including the header row."
- a_bonseyMay 18, 2022Brass Contributor
That worked brilliantly and looked so obvious when you laid it out like that.
Just couldn't see the woods for the trees as they say!!
Thanks again
- a_bonseyMay 18, 2022Brass Contributor
Hi,
I tried tweaking the vba slightly to see what a debug/msgbox would should for the cell(i,1). In the debug (and Msgbox) I get a 'rectangular box' appearing (see attached image).
Are you able to tell me what causes this to appear?
- May 18, 2022
a_bonsey The range of an empty cell in a Word table has a length of 2. Therefore, the code checks to see if the length of the range of each cell is < 3. If it is, then the row is not valid according to your nomenclature.
In the Immediate Window, you should enter
MsgBox Len(Selection.Cells(1).Range)
when the selection is in a cell.