Niles,
Imagine a list like this:
01 - joe
02 - jane
03 - jane
04 - jim
05 - steve
06 - lisa
07 - lisa
08 - tom
09 - steve
10 - steve
If you move forward through the list, you have a problem because your “spot” in the list keeps changing as the list shrinks.
When you delete the first Jane, your iRow value is 1. (2-1 because listboxes are zero-based)
On the next iteration of the loop, your iRow is 2 (next value), but… you’re now on Jim (row 4) because you deleted one of the Janes. So you’re iRow value is no longer in sync with the list, it should be 3 (4-1) but it’s 2 instead.
By the time you get to the two Steves at the end, your iRow will be up to 6 and when you try too iterate again, you’re going to get an out of bounds error because you’re asking for row 8 (iRow = 7) and no row 8 exists at this point.
01 - joe
02 - jane
03 - jim
04 - steve
05 - lisa
06 - tom
07 - steve
So instead, start at the end of the list and count down.
Since you’re “counting down”, you don’t need to worry about if the list gets shorter. You’re never going to go “out of bounds” because iRow is always decreasing.
Taking the same list:
01 - joe
02 - jane
03 - jane
04 - jim
05 - steve
06 - lisa
07 - lisa
08 - tom
09 - steve
10 - steve
Now when you delete steve at row 10, your next iRow value is 8 (9-1), which is still a valid value, when you delete lisa, your next iRow value is 5 (6-1) which is still fine.
You’re never going to try and fetch a row which doesn’t exist anymore, because you’re deleting the last duplicate row in the listbox first in your for…next loop.
Even if you were deleting every row in the listbox, there’s still be no OOB exception since the last row you’d delete would be the first row in the listbox. (iRow = 0)
So getting back to your code, you’d want to do something like this:
dim iLastRow as Integer = (VENDORS_LISTBOX.ListCount - 1)
dim iLastColumn as Integer = (VENDORS_LISTBOX.ColumnCount - 1)
//for each row, **starting from the last**
for iRow as integer = iLastRow DownTo 0
//check every column, they *all* must match row above for the row to be a duplicate
//NOT deleting Listbox's columns, so ok to iterate through columns from first to last
for iColumn as Integer = 0 to iLastColumn
if VENDORS_LISTBOX.Cell(iRow, iColumn) <> VENDORS_LISTBOX.Cell(iNextRow, iColumn) then
//if *any* column doesn't match, skip to the next iteration of the loop
continue for iRow
else
//this row is identical to the row "above" it, so remove this row
VENDORS_LISTBOX.RemoveRow(iRow)`
end if
next
to remove the duplicate rows. <this is forum code - might have bugs!>
HTH,
Anthony