calculation on a column in a table.

G

Gerry Abbott

Hi All,

Im new to the forum, so please be patient if this question has been asked
before.

I wish to calculate the contents of a column in a table (add the values),
and display the results on the document independent of the table. I wish to
be able to do this independent of how many rows there are in the table, and
some values may be blank.

Thanks in advance.
 
G

Gerry Abbott

Hi all again,
Think I have an approach but need help.

I intend to get the number of rows from the table object,
Then for the required column, iteratively sum the cell contents.

However i'm running into difficulty in returning the contents of the cell,
then converting to numeric.

How can i return the contents of the cell rather than the cell reference.?


Any advice welcome.

------------------------------
Sub GerryTable()
'
' GerryTable Macro
' Macro recorded 09/11/03 by Gerry
'

Dim myTable As Table
Dim myVar As Variant
Dim nRows as integer
Dim nCols as Integer

Set myTable = ActiveDocument.Tables(1)
With myTable
nRows = .Rows.Count
nCols= .Columns.Count

myVar = .Rows(i).Cells(2)

End With


end sub
 
G

Gerry Abbott

Found a solution.
See below.
Hope someone finds this useful.

This sums the values in column 3, and puts the result into a bookmark,
"Total".


------------------------------------------
Sub GerryTable()
'
' GerryTable Macro
' Macro recorded 09/11/03 by Gerry


Dim myTable As Table
Dim nTotal As Double
Dim i As Integer

Set myTable = ActiveDocument.Tables(2)
With myTable
For i = 2 To .Rows.Count
nTotal = nTotal + celValue(myTable, i, 3)
Next i
End With

Set myTable = Nothing


With myTable
ActiveDocument.Bookmarks.Item("total").Range.Text = Int(nTotal * 100 +
0.5) / 100

End With


Debug.Print nTotal


End Sub


Function celValue(mTable As Table, mRow As Integer, mCol As Integer) As
Double

With mTable
celValue = Val(Left(.Cell(mRow, mCol).Range.Text, Len(.Cell(mRow,
mCol).Range.Text) - 2))
End With

End Function
----------------------------------------



Gerry Abbott said:
Hi all again,
Think I have an approach but need help.

I intend to get the number of rows from the table object,
Then for the required column, iteratively sum the cell contents.

However i'm running into difficulty in returning the contents of the cell,
then converting to numeric.

How can i return the contents of the cell rather than the cell reference.?


Any advice welcome.

------------------------------
Sub GerryTable()
'
' GerryTable Macro
' Macro recorded 09/11/03 by Gerry
'

Dim myTable As Table
Dim myVar As Variant
Dim nRows as integer
Dim nCols as Integer

Set myTable = ActiveDocument.Tables(1)
With myTable
nRows = .Rows.Count
nCols= .Columns.Count

myVar = .Rows(i).Cells(2)

End With


end sub
 

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