Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Decrease iterations of loop after an array item has been deleted?

Tags:

arrays

loops

vba

In VBA for Excel:

For i = 0 To UBound(artMaster)
    For j = i To UBound(artMaster)
        If i <> j And artMaster(i).VDN = artMaster(j).VDN Then
            Call DeleteArrayItem(artMaster, j)
        End If
    Next j
Next i

How can I decrease the iterations of the loop after I have deleted one of the array items?

like image 595
user1283776 Avatar asked Apr 27 '26 10:04

user1283776


1 Answers

You can subtract 1 from your iterator. But that can be problematic and makes the code harder to understand.

Perhaps a better approach is to loop from the last item to the first (step -1). This way, your iterator remains valid as you delete items.

like image 200
Jonathan Wood Avatar answered Apr 29 '26 20:04

Jonathan Wood