Today, I came across a code example like the following:
Option Explicit Sub MySub() Dim iCount As Integer For iCount = 0 To 100 Debug.Print "Arbitrary Code Here" If iCount Mod 2 = 0 Then Dim myString As String 'This is the line I'm curious about myString = iCount Debug.Print myString End If Next iCount End Sub
Once a variable goes in scope in VB6 and/or VBA, it stays in scope throughout the procedure.
So, is it a bad programming practice to declare a variable in a loop? Is it better to declare myString either at the top of the procedure or, at the very least, above the FOR LOOP?