read text from a table cell

J

juan

Hi, I need to go through a column of a table and sum all its values.
Something like in text controls refered as total=total+.textbox.value...how
to extract the value of a cell.

thanks
Juan Uribe
 
T

Tony Strazzeri

Hi, I need to go through a column of a table and sum all its values.
Something like in text controls refered as total=total+.textbox.value...how
to extract the value of a cell.

thanks
Juan Uribe

Hi Juan,

If you know the table and column you can set an object reference to
simplify things.
eg

Dim myTableColumn As Column
Dim ThisTableNum As Single
Dim ThisTableColumn As Single

ThisTableNum = 2 'lets work with table 2
ThisTableColumn = 2 'lets work with column 2

Set myTableColumn =
ActiveDocument.Tables(ThisTableNum).Columns(ThisTableColumn)

Dim x
Dim thisCell As Cell
Dim Tot
Dim strCellText As String

For x = 1 To myTableColumn.Cells.Count
Set thisCell = myTableColumn.Cells(x)
strCellText = thisCell.Range.Text

'strip the cell marker which at the end of each cell
strCellText = Left(strCellText, Len(strCellText) - 2)
Tot = Tot + Val(strCellText)
Next


Hope this helps.

Cheers
TonyS.
 
J

juan

Thanks Tony, perfect,
....now I am trying to find out how in VBA is to take away the comma from
strCellText with a builtin function as in VFoxPro without going char by
char...
 
T

Tony Strazzeri

Hi Juan,
Yes, that can be tricky. You would expect that using Val would work. But as
you can see below it doesn't.


Dim strNumberString As String 'assuming the value is a string say in
this var
Dim NumVal As Double ' or Single etc
strNumberString = "123,000.23"

NumVal = Val(strNumberString) 'Wrong answer

NumVal = Val(Format(strNumberString, "General Number")) 'right answer


Cheers
TonyS.
 

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