summing numbers in a column

harry5568

Member
Hello,
Can Anybody tell me how to sum numbers in a column of a table,for e.g
2
4
3
8
9
10
Answer in another cell of the table should be 36. Thx..Harry:sign0085:
 

harry5568

Member
Well..I found the answer myself...Thanx Anyways..Cheers..


Sub Globals
'Declare the global variables here.
Dim x
Dim inx

End Sub

Sub App_Start
Form1.Show
Table1.Clear
table1.AddCol(cNumber, "men",20)
table1.AddCol(cNumber, "women",20)
table1.AddRow(10,20)
table1.AddRow(10,15)
table1.AddRow(15,7)
End Sub


Sub Button1_Click
For inx = 0 To Table1.RowCount - 1
x = x + Table1.Cell("men",inx)
Next
TextBox1.Text = x
End Sub
 

harry5568

Member
On pressing Button1,I do get the total of the column.
But on pressing it again,the column numbers once again get added to the existing total,which is wrong!
Can somebody tell me how to change the For Next statement to correct this..Thanx
 

harry5568

Member
Ha Ha....found the solution

Sub Button1_Click
x=0
For inx = 0 To Table1.RowCount - 1
x = x + Table1.Cell("men",inx)
Next
TextBox1.Text = x
End Sub
 

mjcoon

Well-Known Member
Licensed User
Ha Ha....found the solution

Sub Button1_Click
x=0
For inx = 0 To Table1.RowCount - 1
x = x + Table1.Cell("men",inx)
Next
TextBox1.Text = x
End Sub

Hmm, assuming that you have not turned off the warning of variables being used before being set, that may mean that the check misses that this is happening. After all, in the original version the first reference to the value of x takes place before the sum is assigned back to it, so the warning should have appeared...

Mike.
 

mjcoon

Well-Known Member
Licensed User
I think x must be a global variable as if it was local it would be initialised to 0 on entry to the event Sub.

Ah... I had hoped that no-one would use an anonymous sort of name like "x" for a global variable. It could lead to confusion later...

Mike.
 

harry5568

Member
hi mjcoon and agraham...yes...x is a global variable in my prog...if u see my 2nd post, i have declared it as a global var as under

Sub Globals
'Declare the global variables here.
Dim x
Dim inx

Yeah...x is not a little correct to use...i'm still learning prog..actually a commercial pilot by profession..i find basic4ppc very logical and a good way to start learning...and of course i'll pick up all the good things frm u guys...thx
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…