Trouble with Dir()

TurtleM

New Member
Joined
Sep 7, 2017
Messages
5
Hello everyone,

I am having trouble with the Dir function. This is the first time I have used it, and I don't think I understand where it searches.

When I type:

Dim Path As String
Path = Application.ActiveWorkbook.Path
MsgBox Path

The Messagebox returns a path.

However adding Dir:

Dim Path As String
Dim str As String
Path = Application.ActiveWorkbook.Path
MsgBox Path
str =Dir(Path)
MsgBox str

The second MessageBox is empty.

Why is Dir not able to use the given path?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
What do you expect Dir to return?

All you have passed it is a directory, if you wanted to use it to find files you would need to pass it a filename or pattern.

For example this will return the name of the first file found in the same directory as the active workbook.
Code:
Dim Path As String
Dim str As String

    Path = Application.ActiveWorkbook.Path
    MsgBox Path
    str = Dir(Path & "\*.*")
    MsgBox str
 
Upvote 0
Thank you for this explanation. I was trying to copy an example from the web and it didn't work, but your code does. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top