By adding 2 lines instead of one, you moved the rows about.
By introducing a new variable called total , and then reporting on the value of runningtot instead, the counting was not being reported.
On the assumption that maybe you now want both a count of the rows, and the sum of the values, here is working code once more.
You will see that all that was needed to sum the values was to switch from +1 to cdbl(…)
The extra variables I have added are in order to maintain 2 different running values.
Listbox1.AddRow "A01","1"
Listbox1.AddRow "A01","1"
Listbox1.AddRow "B02","1"
Listbox1.AddRow "B02","1"
Listbox1.AddRow "C03","2"
Listbox1.AddRow "C03","2"
Listbox1.AddRow "C03","2"
Listbox1.AddRow "D04","2"
Listbox1.AddRow "D04","2"
Listbox1.AddRow "01","2"
Listbox1.AddRow "01","2"
Listbox1.AddRow "02","2"
Listbox1.AddRow "02","2"
Listbox1.AddRow "02","2"
Listbox1.AddRow "02","2"
Listbox1.AddRow "03","2"
Listbox1.AddRow "03","2"
Listbox1.AddRow "04","2"
Listbox1.AddRow "04","2"
dim last_text as string
dim runningtot as integer
dim runningsum as double
ListBox1.addrow "Dummy"
for x as integer = 0 to ListBox1.LastRowIndex
if ListBox1.CellValueAt(x, 0) <> last_text then
//found a new value
ListBox1.celltag(x,0) = cstr(runningtot) //tag the row as the last one
ListBox1.celltag(x,1) = cstr(runningsum)
runningtot = 1
runningsum = cdbl(ListBox1.CellValueAt(x, 1) )
last_text = ListBox1.CellValueAt(x, 0)
else
//same as before, so add one
runningtot = runningtot + 1
runningsum = runningsum + cdbl(ListBox1.CellValueAt(x, 1) )
end if
next
dim t,s as string
for x as integer = ListBox1.LastRowIndex downto 1
if ListBox1.celltag(x,0) > "" then
//for simplicity, I grab the values we need to display **before** adding a row.
//this means you are free to add a blank row without breaking the code
//if you wish.
t= ListBox1.celltag(x,0)
s = ListBox1.celltag(x,1)
ListBox1.AddRowAt(x, "Tot: " + t)
ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, 1) = "Sum : " +s
end if
next
ListBox1.RemoveRowAt(ListBox1.LastRowIndex) //remove the dummy row