Find first empty row of a table?

R

Robert Crandal

One of our Word 2007 document templates contains a table of
data (which is 15 rows by 5 columns in dimension). Using
VBA, how can I determine (starting from the top) which row
is the first empty row?? For example, if the first 4 rows of the
table contain data, that means that row 5 is the "first empty row".

Finally, once I get a reference to the first empty row, how can
I fill that entire row with data of my choice??

thank you
 
G

Greg Maxey

An empty cell has a range length = 2 so does and empty row (the end of cell
mark and end of row mark each have a range length = 2. So a 5 column row
will have a range lenght of 12

Sub ScratchMaco()
Dim oRow As Row
For Each oRow In ActiveDocument.Tables(1).Rows
If Len(oRow.Range) = 12 Then
With oRow
.Cells(1).Range.Text = "Text of your choice"
.Cells(2).Range.Text = "Text of your choice"
.Cells(3).Range.Text = "Text of your choice"
.Cells(4).Range.Text = "Text of your choice"
.Cells(5).Range.Text = "Text of your choice"
End With
Exit For
End If
Next
End Sub
 
M

macropod

Hi Robert,

Try something along these lines:
Sub Test()
Dim i, j As Integer, bEmpty As Boolean
With ActiveDocument.Tables(1)
For i = 1 To .Rows.Count
bEmpty = True
For j = 1 To .Columns.Count
If Len(.Cell(i, j).Range.Text) > 2 Then bEmpty = False
Next
If bEmpty = True Then
MsgBox "Row " & i & " is the first empty row."
Exit Sub
End If
Next
MsgBox "There is no empty row."
End With
End Sub
 
K

Klaus Linke

Greg Maxey said:
An empty cell has a range length = 2 so does and empty row (the end of
cell mark and end of row mark each have a range length = 2. So a 5 column
row will have a range lenght of 12

Also, both the end-of-cell-marker and the end-of-row-marker consist of
Chr(13) & Chr(7), so if you delete those from the string and are then left
with an empty string, then the row was empty.

? Len(Replace(myRow.Range.Text,Chr(13) & Chr(7),"")) = 0
True

If you want to ignore paragraph marks and other whitespace, you could
replace those with nothing too, before testing whether the remaining string
is empty.

Klaus
 

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