Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Tech Assist

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 11 Dec 2004 08:11 GMT
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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Jonathan West - 11 Dec 2004 09:31 GMT
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

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

> 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
[quoted text clipped - 31 lines]
>
> End Sub
Greg Maxey - 11 Dec 2004 15:39 GMT
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.

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi Greg
>
[quoted text clipped - 68 lines]
>> Greg Maxey/Word MVP
>> A Peer in Peer to Peer Support
Greg Maxey - 11 Dec 2004 15:51 GMT
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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi Greg
>
[quoted text clipped - 68 lines]
>> Greg Maxey/Word MVP
>> A Peer in Peer to Peer Support
Jonathan West - 11 Dec 2004 22:22 GMT
> Jonathan,
>
[quoted text clipped - 3 lines]
> 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

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Greg Maxey - 12 Dec 2004 00:31 GMT
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.

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

>> Jonathan,
>>
[quoted text clipped - 59 lines]
>    "Count = " & iCount & " "
> End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.