finding empty row/column in table

J

James

Is there a way, other than brute force (looping) to determine whether a
specified row or column is empty of data?
Tks.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < James > écrivait :
In this message said:
Is there a way, other than brute force (looping) to determine whether a
specified row or column is empty of data?

For Rows, try something like:

'_______________________________________
With ActiveDocument.Tables(1).Rows(1)
If (Len(.Range.Text) / 2) - 1 = .Cells.Count Then
MsgBox "Current Row is empty of data!"
End If
End With
'_______________________________________

For columns, unfortunately there isn't a range property for them (if you try
to set up a column range, Word will include all the cells to the right of
the first cell, all the rows in between and all the cells to the left of the
last cell. So, if you try with the first column, you get the whole table.)
This is why you sometimes get funny results when using SUM fields that refer
to columns that are bookmarked.
Unless someone else has a trick, it looks like you are going to have to loop
through each cell.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

James

Merci encore, Jean-Guy.

Would you mind to take a minute to explain how your solution works? It
seems cryptic to me.

tks
James
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < James > écrivait :
In this message said:
Merci encore, Jean-Guy.

Would you mind to take a minute to explain how your solution works? It
seems cryptic to me.

Empty cells always contain at least a two-character string (the ¤ in each
cell is counted as one character by Word, but two in VB string
manipulations).
So, an empty row will contain nothing but those ¤. If I count how many of
these I have and divide by the number of cell I should get the same number.

With ActiveDocument.Tables(1).Rows(1)
If (Len(.Range.Text) / 2) - 1 = .Cells.Count Then

Len(.Range.Text) = the length of the string in the row, (.Range.Text refers
to ActiveDocument.Tables(1).Rows(1) because of the key word With, everywhere
(under With) that I start with a ".", it is as if I continued from
ActiveDocument.Tables(1).Rows(1). It makes the code easier to read, the
lines shorter, uses up less memory, and in long macros it speeds up the
execution somewhat.)
BUT, .Range.Text (The text in the Range of the target row, Row #1 in this
case) also includes the end of row marker as well (the ¤ outside the table,
at the end of the row)
SO, (Len(.Range.Text) / 2) - 1 is actually counting the number of character
in the string, dividing by two (Len is a VB string manipulator, so it will
count every ¤ as two) and then I subtract the end of row marker.
IF the row is truly empty, the result will be the same as .Cells.Count, the
number of cells in the target row.

Phiew! I hope it makes sense!?!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

James

Could you get me started on looping through columns - I assume I'll
have to take out 2 characters per cell, but beyond that....???
Tks
 
J

James

If we were dealing with a 4x4 table and the first row consisted of a
table title where the cells are all merged into one, is there any way
of deleting one of the columns without deleting that top table title?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < James > écrivait :
In this message said:
If we were dealing with a 4x4 table and the first row consisted of a
table title where the cells are all merged into one, is there any way
of deleting one of the columns without deleting that top table title?

I think the easiest would be to split the table just below the first row,
handle the "new" second table, then delete the ¶ between the two tables to
rejoin them. If you are deleting columns, you are going to have to resize
the remaining column so that they fit the merged first row.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < James > écrivait :
In this message said:
Could you get me started on looping through columns - I assume I'll
have to take out 2 characters per cell, but beyond that....???
Tks


'_______________________________________
Dim MyCol as Column

For Each MyCol in ActiveDocument.Tables(1).Columns

'Do your stuff
MyCol.Delete

Next
'_______________________________________

Or

'_______________________________________
Dim i as Long

With ActiveDocument.Tables(1)
For i = 1 to .Columns.Count

With .Column(i)

'Do your stuff

End With

Next
End With
'_______________________________________

But if you have merged cells, you might have surprising results!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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