View Single Post
  #7   Report Post  
Greg Maxey
 
Posts: n/a
Default

Brad,

Refined a little and undoubtably still rough. Still this version may make
it a little easier and avoid some errors. Basically I added a string check
so if you run the setup macro on a table already calculated it will find the
% in the first cell string and jump to the Refresh macro. This will prevent
you adding two more rows by accident. I am sure this could be abbreviated
and refined some more, but if you are only operting on single tables it may
do. Let me know.

Sub SetupTable()
Dim iCol1Count As Long
Dim iCol2Count As Long
Dim oCell As Cell
Dim oCol As Column
Dim oTable As Table
Dim rChk As Range

If Selection.Information(wdWithInTable) Then
Set oTable = Selection.Tables(1)
Set rChk = oTable.Cell(1, 1).Range
If InStr(rChk, "%") Then
RefreshTable
Exit Sub
End If
oTable.Rows(1).Select
Selection.InsertRowsAbove 2
Selection.Collapse
Set oCol = oTable.Columns(1)
iCol1Count = 0
For Each oCell In oCol.Cells
If oCell.Range.Characters.Count 1 Then
iCol1Count = iCol1Count + 1
End If
Next
Set oCol = Nothing
oTable.Cell(2, 1).Range.Text = iCol1Count
Set oCol = oTable.Columns(2)
iCol2Count = 0
For Each oCell In oCol.Cells
If oCell.Range.Characters.Count 1 Then
iCol2Count = iCol2Count + 1
End If
Next
Set oCol = Nothing
oTable.Cell(2, 2).Range.Text = iCol2Count
oTable.Cell(1, 1).Range.Text = "" & Round((iCol1Count / _
iCol2Count) * 100, 2) & " %"
Else: MsgBox "Put the cursor in a table to calculate"
End If
End Sub
Sub RefreshTable()
Dim iCol1Count As Long
Dim iCol2Count As Long
Dim oCell As Cell
Dim oCol As Column
Dim oTable As Table

If Selection.Information(wdWithInTable) Then
Set oTable = Selection.Tables(1)
Set oCol = oTable.Columns(1)
iCol1Count = -2
For Each oCell In oCol.Cells
If oCell.Range.Characters.Count 1 Then
iCol1Count = iCol1Count + 1
End If
Next
Set oCol = Nothing
oTable.Cell(2, 1).Range.Text = iCol1Count
Set oCol = oTable.Columns(2)
iCol2Count = -1
For Each oCell In oCol.Cells
If oCell.Range.Characters.Count 1 Then
iCol2Count = iCol2Count + 1
End If
Next
Set oCol = Nothing
oTable.Cell(2, 2).Range.Text = iCol2Count
oTable.Cell(1, 1).Range.Text = "" & Round((iCol1Count / _
iCol2Count) * 100, 2) & " %"
Else: MsgBox "Put the cursor in a the table to refresh"
End If
End Sub

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

Brad H. wrote:
Hey, Greg.
Sorry it's been awhile. I did get the cleaner version of your macro
via email. It's great. A couple of things I wasn't clear on, though.

Rather than inserting text for column counts and the percentage,
these need to be a field codes. (Subsequent adding or deleting to the
list is why. Then use Ctrl+F9 to update the fields).

Second, rather than the macro running on all the tables in the
document, I want it to run on the one my cursor is in. The scenario
is that I paste a 2-column table, then put the cursor at the top of
the first column and run the macro. For example, here's a simple
freshly pasted table (the table has 2 columns and 3 rows):

Unit1 Unit1
Unit3 Unit2
Unit3

So then I put my cursor in the upper left cell and run the macro
which:
1. Adds a row above
2. Inserts a field code in each column header that counts the Units
below.
3. Adds another row above.
4. Inserts a field code that calculates the percentage of column A to
column B.
The result is this (the table has 2 columns and 5 rows):

67%
2 3
Unit1 Unit1
Unit3 Unit2
Unit3

What I'm after may not be possible with field codes in all three
spots--cell A2 in particular, because there is a blank cell in the
column, so I may have to live with text inserted instead of a field
code for that one. The rest is doable, right?

Brad H.