1

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?

braX
  • 11,506
  • 5
  • 20
  • 33
  • I ran your code and it worked just fine for me. I put it in a subroutine tho... did you try that? You cant just put all of that into the immediate window... – braX Jan 07 '20 at 02:26

1 Answers1

1

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.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • You can, however, wrap the OP's code in a routine and execute from there, where `Debug.Print` will print to the immediate window. As I am sure you are aware... – Dean Jan 07 '20 at 07:44
  • @Dean OP was pretty clearly about using the immediate window though. Or maybe I misread, but I was under the impression OP was confused about how it works. – Mathieu Guindon Jan 07 '20 at 13:33