vbadirectoryimmediate-window

How to show directories in vba's immediate window?


I want to list all directories in c: disk in vba's immediate window,write the below code in immediate window:

Dim FileName As String
FileName = Dir("C:\", vbDirectory)

Do While FileName <> ""
    Debug.Print FileName
    FileName = Dir()
Loop

Now to click enter,

enter image description here

How to fix my vba code to show all directories in c: in vba's immediate window?


Solution

  • The immediate window does not execute a script; it runs individual statements, immediately as you hit Enter (whether you just typed the line or not). You can't script a sequence of such executable statements in that box, the statements must be self-contained.

    But you can cheat, by using the : instruction separator:

    fn=dir("C:\",vbdirectory):do while fn<>"":?fn:fn=dir:loop
    

    Keep in mind that the window holds no more than 255 lines - you'd have to write to a file to output more than that and be able to view it all.