File and pathname with spaces

Charles Bushby

New Member
Joined
Aug 10, 2005
Messages
42
Below is an extract of some code that fails because there are spaces in the pathname and the filename (e.g. "AR timesheet.xlsm". I have tried various options such as Chr(34), """"" and %20 but can't seem to get it to work. If I take out the spaces in the path and filename then the code works fine but for various reasons I have to have spaces.


Dim FolderName As String, wbName As String
FolderName = "\\sw-dc2\Shared\Central Region\Time sheets"
wbName = Dir(FolderName & "" & "*.xlsm")
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I suspect it's not the spaces, but the fact that you are missing a \ from the end of the folder name ie
FolderName = "\\sw-dc2\Shared\Central Region\Time sheets \ "
Then again it just stripped out the final\ in my suggestion & may have done the same for you
 
Last edited:
Upvote 0
In that case I'm at a loss.
I've regularly used files & directories which have spaces in their names without any problems.
 
Upvote 0
It does seem odd that you would leave out the path separator between the folder path and the file name.
What is the code supposed to do? Can't tell from the snippet. Is the path a local folder, a file share, or a remote location? What about other files in the folder? Why are you using dir() here when you want a particular file, rather than using the file name? Note that in general I wouldn't recommend DIR() with unc paths - probably using a file system object is better. I'm not really sure if DIR works with UNC paths but I don't really know for sure.
 
Last edited:
Upvote 0
I have sorted it now - I had accidentally truncated the pathname. For reference this now works:

FolderName = "\\sw-dc2\Shared\Central Region\Time sheets\Current time sheets"
wbName = Dir(FolderName & "*.xlsm")

The Dir seems to work fine.

Thanks for you help
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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