Summing the numeric values of the selected rows:

Hi guys

This sum value of the first column(0)

Question is how i can sum de value of the column(11)?

Dim i, total as Integer
For i=0 to ListBox1.ListCount-1
If ListBox1.Selected(i) then
total=total+Val(ListBox1.List(i))
End if
Next

Thanks for the tip

Alan

The columns are 0-based. If you want the 11th column as it appears on screen, then
ListBox1.Cell(i, 10)

Hi guys

Yes i did to but im getting this error which why im asking to more experts: " There is more than one item with this name and is not clear which this refers…

I TIm

the column 11 has only integer numbers so no strings.

Any suggestion?

You will be fine with either Val or CDbl
Could you post the code where you are getting the error?

Hi Roger

Dim i, total as Integer

For i=0 to ListBox2.ListCount -1
If ListBox2.Selected(i) then
total=total+Val(ListBox2.List(i,11))
TextField1.Text= format(total, “\$###,###,##0.00”)
End if
Next

the code is the Change event of my listbox

Thanks

Works fine with the first column(0) getting the sum of the row ID …

Take this line…
TextField1.Text= format(total, “\$###,###,##0.00”)
out of the for…next loop.
I assume you only want to fill the textfield once.

mmmm but the loop has nothing to see with the error that i’m getting.

so this is the code token from xojo wiki , but when i put the number of column im getting the error above

Dim i, total as Integer

For i=0 to ListBox2.ListCount -1
If ListBox2.Selected(i) then
total=total+Val(ListBox2.List(i))

End if

Next

Why?

  total=total+Val(ListBox2.List(i,11)) <<<<<<<<<< this is wrong

LIST does not take two parameters (http://documentation.xojo.com/index.php/ListBox.List)

use CELL instead of LIST http://documentation.xojo.com/index.php/ListBox.Cell

I’m almost positive if you got that code to work it wouldn’t do what you wanted if you truly have 11 columns.

You’re going down each row to see if it’s selected, then when you find a selected row you try to get the entire column value of whatever row you’re on. Let’s say cell 2, 5 was selected. Your code will find row 2 selected, and try to get the value of column 2, even though row 2 is selected in column 5.

Hi Norman

Many thanks now works fine!!!

Tim

Thank you!