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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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
what exactly are you trying to do?
 
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,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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