VBA: how to catch DIR error

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,881
Office Version
  1. 2010
Platform
  1. Windows
I thought the DIR function would return a null string if it could not find the file or directory. In my case, it returns "bad file name or number."

How do I catch this error? I tried iserr(dir(....)) and iserror(dir(....)). Both still returned "bad file name or number".

My code looks like this:

if dir(....) <> "" then 'found the file
do something

If I can catch the error, I can modify the code to suit my need. Thanks.
 
So Excel is raising an error, that is, displaying an error message in a dialog while suspending macro execution? If so, try error trapping.

Code:
On Error Resume Next
stringvariable = ""
stringvariable = Dir("\\a\long\unc\path", vbDirectory)
If Err.Number <> 0 Then
  'effectively invalid unc server, take whatever action you deem appropriate
  Err.Clear
End If
If stringvariable <> "" Then
  'found the directory, so do what you need to do
End If
Thanks. It works.
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I can't get Dir() to work with a UNC file path in VBA. Maybe VBA can't handle this job (?).

I.e., at the command prompt:
DIR \\Corpfs\ACCTG --> works

in VBA
Dir ("\\Corpfs\ACCTG") --> bad file name or number

So if the share exists but Dir can't find it in VBA then we can't use Dir() for this job.
Thanks for the reply. I'm running Excel 2010 on Windows 7. UNC file path works for me if, of course, the file and share exist.
 
Upvote 0
I'm running Excel 2010 on Windows 7. UNC file path works for me if, of course, the file and share exist.

Do you mean it works for you in *VBA* with the DIR() command? Have you tried using the FSO library instead of Dir()?
 
Upvote 0
Yes. The dir() command works in VBA. I also tried you FSO method. It works also. Thank you.
 
Upvote 0
I get the "bad file name or number" error with:
Code:
Dir("\\Jupiter\Dataset4\", vbDirectory)
when that server is no longer on the local network.

It takes a few seconds for the error to come up, so I assume the dir command is waiting on a response from the DNS server.
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,327
Members
451,697
Latest member
pedroDH

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