How to sum values into selection cells?

A

avkokin

Hello.
There is 3 tables. Every table has some rows and columns. Every column
has some cells with values (numbers). I selecting all cells from one
column of first table and I should to get autosum it. But this autosum
should be save as variable but not adds as new cell or row.
Next I select other cells from one column from second table and I want
autosum it and add this sum to sum which I got from first table.
How?
Thank you very much.
 
H

Helmut Weber

Hi Anton,

I think you got to store the intermediate result somewhere,
e.g. in a documentvariable, like that:

Sub Test001x()
Dim d01 As Double
Dim dNmb As Double
Dim ocll As Cell
Dim sTmp As String
For Each ocll In Selection.Cells
sTmp = Left(ocll.Range.Text, Len(ocll.Range.Text) - 2)
dNmb = dNmb + CDbl(sTmp)
Next
MsgBox dNmb
ActiveDocument.Variables("Sum") = _
ActiveDocument.Variables("Sum").Value + dNmb
MsgBox ActiveDocument.Variables("Sum").Value
End Sub

Sub InitSum()
ActiveDocument.Variables("Sum") = 0.5
MsgBox ActiveDocument.Variables("Sum")
End Sub

Sub CloseSum()
ActiveDocument.Variables("Sum") = 0
MsgBox ActiveDocument.Variables("Sum")
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

avkokin

Hi Helmut! Thank you very much. It works but I got error 5825 "The
object was deleted" for line "ActiveDocument.Variables("Sum") =
ActiveDocument.Variables("Sum").Value + dNmb" in the end. And autosum
for double table I am not getting.
Why?
 
A

avkokin

I found that line "MsgBox dNmb" is source this error. I was comment it
and all OK. Thank you very much, Helmut.
 
H

Helmut Weber

Hi Anton,

Sub InitSum()
ActiveDocument.Variables("Sum") = 0.5
MsgBox ActiveDocument.Variables("Sum")
End Sub

watch out,
of course to create the documentvariable
you wouldn't set its value to 0.5, but to 0.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

and some advice more,

You have to create the documentvariable at first,
using the macro "InitSum".
The error message you got tells you,
that the variable does not exist.

Probably you tried "Initsum" in between,
then deleted the line Msgbox ...
then ran the main macro,
and the error didn't occur any more.
But it has nothing to do with the line "msgbox ..."

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

avkokin

Helmut, I change documentvariable on simple variable. I use code:
Option Explicit
Public sItog As Double

Sub raschetItog()
Dim d01 As Double
Dim dNmb As Double
Dim oCell As Cell
Dim sTmp As String

For Each oCell In Selection.Cells
sTmp = Left(oCell.Range.Text, Len(oCell.Range.Text) - 2)
dNmb = dNmb + CDbl(sTmp)
Next
'ActiveDocument.Variables("Sum") = ActiveDocument.Variables
("Sum").Value + dNmb
sItog = sItog + dNmb
End Sub

Sub insertItog()
Selection.Text = sItog
sItog = 0
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