Tech Assist

G

Greg Maxey

An OP asked how to count and display the number of cells "in use" in a table
column. I monkeyed around with a macro which works "part time." I put the
cursor in the table cell and run the macro and it reports the number of
cells used in that row. My problem is that it only works for the first
table in the document. If I add a table and run the macro, the result is
put in the first table. Here is what I have with comments:

Sub Test()
Dim i As Long, j As Long, k As Long ' k is supposed to be the Table index
:-(
Dim iCount As Long
Dim oCell As Cell
Dim oCol As Column

If Selection.Information(wdWithInTable) = True Then
'k = Selection.Cells(1).Tables 'I can't figure out how to determine the
table index
i = Selection.Cells(1).RowIndex
j = Selection.Cells(1).ColumnIndex
End If
For Each oCell In Selection.Tables(1).Columns(j).Cells
If oCell.Range.Characters.Count > 1 Then
iCount = iCount + 1
End If
Next
ActiveDocument.Tables(k).Cell(i, j).Range.InsertBefore "Count = " & iCount &
" "
'the above line works when .Tables(k) is .Table(1) but the result is alwasy
in Table 1. I want the result in the Table that has focus.
'The following line is what I think should work if I could determine k.

ActiveDocument.Tables(k).Cell(i, j).Range.InsertBefore "Count = " & iCount &
" "


End Sub
 
J

Jonathan West

Hi Greg

The reason for your problem is that you are getting the data out of
Selection.Tables(1), and putting the result into ActiveDocument.Tables(1)

This is one of those occasions where using object variables can be quite a
good idea, not just to make the code more efficient but also easier to read.
Here's the macro again, but with an object variable defined for the table
you are in and for the column you are checking.

Beware that the macro will fail with an error if the table contains any
merged cells. There is a workaround for that if you need it.

Sub Test()
Dim iCount As Long
Dim oCell As Cell
Dim oCol As Column
Dim oTable As Table

If Selection.Information(wdWithInTable) = True Then
Set oTable = Selection.Tables(1)
Set oCol = Selection.Columns(1)
Else
MsgBox "Selection is not in a table"
Exit Sub
End If
For Each oCell In oCol.Cells
If oCell.Range.Characters.Count > 1 Then
iCount = iCount + 1
End If
Next
oTable.Range.Cells(oTable.Range.Cells.Count).Range.InsertBefore _
"Count = " & iCount & " "
End Sub
 
G

Greg Maxey

Jonathan,

Thanks for the assist. I would be interested in the merged cell work
around, just for the sake of seeing how it would be done.
 
G

Greg Maxey

Jonathan,

I probably wasn't clear on my first post. I wanted the code to apply the
result in the cell containing the insertion point. When I tried your method
the result was put in the last column and last row of the the table. I have
change the code as follows. I would be interested in any comments you have
for improving it:

Sub Test()
Dim iCount As Long
Dim oCell As Cell
Dim oCol As Column
Dim oTable As Table
Dim i As Long, j As Long
If Selection.Information(wdWithInTable) = True Then
Set oTable = Selection.Tables(1)
Set oCol = Selection.Columns(1)
i = Selection.Cells(1).RowIndex
j = Selection.Cells(1).ColumnIndex
Else
MsgBox "Selection is not in a table"
Exit Sub
End If
For Each oCell In oCol.Cells
If oCell.Range.Characters.Count > 1 Then
iCount = iCount + 1
End If
Next
oTable.Cell(i, j).Range.InsertBefore _
"Count = " & iCount & " "
End Sub
 
J

Jonathan West

Greg Maxey said:
Jonathan,

I probably wasn't clear on my first post. I wanted the code to apply the
result in the cell containing the insertion point. When I tried your
method the result was put in the last column and last row of the the
table. I have change the code as follows. I would be interested in any
comments you have for improving it:

You don't need the i & j. Simply insert at the selection, like this:

Sub Test()
Dim iCount As Long
Dim oCell As Cell
Dim oCol As Column
Dim oTable As Table
If Selection.Information(wdWithInTable) = True Then
Set oTable = Selection.Tables(1)
Set oCol = Selection.Columns(1)
Else
MsgBox "Selection is not in a table"
Exit Sub
End If
For Each oCell In oCol.Cells
If oCell.Range.Characters.Count > 1 Then
iCount = iCount + 1
End If
Next
Selection.InsertBefore _
"Count = " & iCount & " "
End Sub

Thanks for the assist. I would be interested in the merged cell work
around, just for the sake of seeing how it would be done.

This requires a bit of knowledge that goes all the way back to Wordbasic and
Word 95. It isn't documented in current versions of Word. The only way to
select a column that includes merged cells (that I know of) is to use the
WordBasic.TableSelectColumn command to select the column, and then iterate
through the cells of the Selection.

Be aware that a horizontally merged cell will register in all the columns it
has been merged from.

Sub Test()
Dim iCount As Long
Dim oCell As Cell
Dim oRange As Range
Set oRange = Selection.Range
If Selection.Information(wdWithInTable) = True Then
WordBasic.TableSelectColumn
Else
MsgBox "Selection is not in a table"
Exit Sub
End If
For Each oCell In Selection.Cells
If oCell.Range.Characters.Count > 1 Then
iCount = iCount + 1
End If
Next
oRange.Select
Selection.InsertBefore _
"Count = " & iCount & " "
End Sub
 
G

Greg Maxey

Jonathan,

Thanks. I should have know that about the selection. Earlier on I was
trying to achieve the cell check by actually moving the selection through
each cell in the column and needed i and j to get back to where the IP
started. Now I see why I don't need them as the IP never moves.
 

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