Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15605

How do I properly exit a For-Next loop that contains a With block?

$
0
0
I have this code here that uses a With block within a For-Next loop, and conditionally exits the loop, but the problem is that this leaves the With block still active (the End With line never gets executed), so my later attempt to ReDim the array causes an error. Is there a correct way to conditionally exit the For-Next loop, so I don't need to wait for the loop to complete, but doesn't leave the array locked?

Code:

Private Type MyType
    Value As Long
End Type

Private Sub Form_Load()
    Dim a() As MyType
    Dim n As Long
    ReDim a(2)
   
    a(0).Value = 1
    a(1).Value = 2
    a(2).Value = 3
   
    For n = 0 To 1
        With a(n)
            If .Value = 2 Then Exit For
        End With
    Next n
   
    ReDim a(0)
    a(0).Value = 123
End Sub


Viewing all articles
Browse latest Browse all 15605

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>