Aşağıda bir örnek koddur. Belirtilen dizinin tüm alt dizinlerini çaprazlar ve hemen pencere içinde dizin ve dosya adlarını yazdırır. Sorgu, programın bu satırdaki "TraversePath" ı "TraversePath yolu & dizini &" \ "" olarak nasıl çağırdığını gösterir? . Bu özyineleşme… bunu kodlamak için daha iyi bir yol var mı? çünkü yinelemeyi doğru bir bağlamda kullanmazsak, daima taşma ihtimali vardır.bu VBA programını yineleme olmadan nasıl yazılır
Sub TraversePath(path As String)
Dim currentPath As String, directory As Variant
Dim dirCollection As Collection
Set dirCollection = New Collection
currentPath = Dir(path, vbDirectory)
'Explore current directory
Do Until currentPath = vbNullString
Debug.Print currentPath
If Left(currentPath, 1) <> "." And _
(GetAttr(path & currentPath) And vbDirectory) = vbDirectory Then
dirCollection.Add currentPath
End If
currentPath = Dir()
Loop
'Explore subsequent directories
For Each directory In dirCollection
Debug.Print "---SubDirectory: " & directory & "---"
'How program is calling/executing below line of code
TraversePath path & directory & "\"
Next directory
End Sub
Sub Testprogram()
TraversePath "D:\VBA\"
End Sub
Bu özyineleme. Bilgisayar bilimlerinde standart bir araçtır. Geçilen ağaçlar bunun için yaygın bir kullanım durumudur. Bakınız https://en.wikipedia.org/wiki/Recursion_(computer_science) –