What is wrong with this statement?

J

Joanne

Hello,
I'm trying to write a macro that simply adds up the values from cells e9 to
e19 in a table. I'm just starting to write the code and this first line
keeps giving me the following error when I compile it: "Expected function or
variable". I don't understand. I thought I could assign a value to a
selection.

Thanks very much for your help


Dim vSum As Variant
Dim i as integer

For i = 9 To 19
vSum = ActiveDocument.Tables(1).Cell(Row:=i, Column:=5).Select
Next i
End Sub
 
J

Joanne

Hello again,

I'm sorry. I figured that one out. This is the code I meant to post:

Public Sub TableTotal()
Dim i As Integer
Dim vSum As Variant
vSum = 0
For i = 9 To 19
vSum = vSum + ActiveDocument.Tables(1).Cell(Row:=i, Column:=5)
Next i
End Sub
It keeps giving me "type mismatch" so I don't know how to add the value of
what's in the cell to the value of vsum. Any help would be greatly
appreciated.
 
T

Tony Jollans

I wonder if you have maybe done similar things in Excel, which is slightly
different.

Tables in Word consist of cells, each cell being, in essence, a mini
document part. A Cell is just that, a cell. The content of a cell, the Range
of that cell, is a string of text (and maybe other characters), including a
trailing cell delimiter. Your type mismatch is because you are trying to add
a cell to a numeric total, which makes no sense. You need to get the numeric
equivalent of the cell content.

This will give you the Range of the Cell:

(cell_reference).Range

This will give you the Text of the Range of the Cell:

(cell_reference).Range.Text

And this will give you the number at the beginning of that text - which is
what you want if the cell contains only numeric characters followed by a
delimiter (always there, although not necessarily visible).

Val((cell_reference).Range.Text)

In your code this would be:

Val(ActiveDocument.Tables(1).Cell(row:=i, Column:=5).Range.Text)
 
J

Jay Freedman

Hello again,

I'm sorry. I figured that one out. This is the code I meant to post:

Public Sub TableTotal()
Dim i As Integer
Dim vSum As Variant
vSum = 0
For i = 9 To 19
vSum = vSum + ActiveDocument.Tables(1).Cell(Row:=i, Column:=5)
Next i
End Sub

It keeps giving me "type mismatch" so I don't know how to add the value of
what's in the cell to the value of vsum. Any help would be greatly
appreciated.

The expression you have, starting with "ActiveDocument", represents a Cell
object. That isn't a number, so VBA doesn't know how to add it to the value of
vSum. That's what "type mismatch" means.

The statement you need is

vSum = vSum + Val(ActiveDocument.Tables(1).Cell(Row:=i, Column:=5).Range.Text)

What this says is "take the cell in row i and column 5, get the Range (the area
of the document) that belongs to that cell, get the text from that range, and
use the Val function to convert it to a number".
 

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