title: Resetting after using Dir and GetAttr
body: <p>I currently have a VBA function that puts the names of all subfolders in a given directory into a ListBox. Intending to further modulize this process, I am splitting this into a function that reads subfolder names into an array, and then another function which populates a ListBox with the array contents. </p>
  
  <p>The segment of code that I have for obtaining subfolder names looks like this:</p>
  
  <pre><code>Sub FoldernamesToListbox(lookin As String)
      directory = Dir$(lookin &amp; "*.*", vbDirectory)
      Do While Len(directory)
          If directory &lt;&gt; "." And directory &lt;&gt; ".." And GetAttr(lookin &amp; directory) And vbDirectory Then
              MyBox.AddItem directory
          End If
          directory = Dir$()
      Loop
  End Sub
  </code></pre>
  
  <p>In order to create a function that reads this into an array I have created an additional function that counts the number of subfolders, very similar to the one above, in order to provide a size to the array, thus avoiding slow "ReDim Preserve" calls.</p>
  
  <pre><code>Function CountFolders(lookinPath As String) As Integer
      j = 0
      directory = Dir$(lookinPath &amp; "*.*", vbDirectory)
      Do While Len(directory)
          If directory &lt;&gt; "." And directory &lt;&gt; ".." And GetAttr(lookinPath &amp; directory) And vbDirectory Then
              j = j + 1
          End If
          directory = Dir$()
      Loop
      CountFolders = j
  End Function
  </code></pre>
  
  <p>However, when I use the counting function within the new function that adds folder names to an array, there seems to be errors with the Dir$() command in the function:</p>
  
  <pre><code>Sub FoldernamesToArray(lookin As String)
      Dim MyArray() As Variant
      ReDim MyArray(CountFolders(lookin))
      directory = Dir$(lookin &amp; "*.*", vbDirectory)
      j = 0
      Do While Len(directory)
          If directory &lt;&gt; "." And directory &lt;&gt; ".." And GetAttr(lookin &amp; directory) And vbDirectory Then
              j = j + 1
              MyArray(j) = directory
          End If
          directory = Dir$()          'ERROR OCCURS HERE
      Loop
  End Sub
  </code></pre>
  
  <p>This seems to be occuring because of the presence of a function call to CountFolders. If I hardcode the number in the function it seems to run fine. What is it about the properties of the Dir$() function or GetAttr that are causing this issue?</p>