# Calculating value on non unique id

Hi,

I have 2 list-boxes,

Listbox1
Data
AW01 0
AW02 0
AW03 0

Listbox2
Data
AW01 1
AW01 1
AW01 1
AW02 1
AW02 1
AW02 1
AW03 1
AW03 1
AW03 1

I tried to calculate data from listbox2 into listbox1 using this code, but the result is not correct,

``````dim o as integer
for o=0 to Listbox1.Listcount -1
dim gg as integer
for gg= Listbox2.Listcount -1 DownTo 0

If Listbox1.cell(o,0)=Listbox2.cell(gg,0) then
dim ox as integer
for ox=0 to ListBox1.Listcount-1
dim total as double
total=total+CDbl(listbox2.cell(o,1))
listbox1.cell(o,1)=str(total)
next
end if
next
next
``````

i am expecting the result are,
AW01 3
AW02 3
AW03 3

any helps

thanks
arief

this is wrong because of the variable scope.

first reset the first list to 0 values.

then read value from cell, add value from other cell, write value to cell.
put your code parts into methods.

Markus is right
put the line â€śdim total as doubleâ€ť at the top of the method
the dim command resets its value to zero

Like this?

``````For o As Integer = 0 To Listbox1.LastRowIndex

For gg As Integer = 0 To Listbox2.LastRowIndex

If Listbox1.CellTextAt(o,0) = Listbox2.CellTextAt(gg,0) Then

Listbox1.CellTextAt(o,1) = Str(Listbox1.CellTextAt(o,1).ToDouble + Listbox2.CellTextAt(gg,1).ToDouble)

End If

Next

Next
``````

Not only what others have said, but variables declared within a forâ€¦next loop only exist within that forâ€¦next loop.

``````For i as Integer = 0 to 10
dim Total as integer
Total = Total + i
Next