VBA - Dir function not working

MisterProzilla

Active Member
Joined
Nov 12, 2015
Messages
264
Hi there,

This is driving me nuts! In my current workbook I have a number of references to another workbook. In the Auto_Open sub, I want to check that the filepath for that external doc is still valid, and return an info message if not so the user can update the links if necessary. As I understand, I should be able to use the Dir function to look for the filepath - if it finds the file, the value returned will be non-blank, if it can't find a match, the value will be blank.

I've tried this code:
Code:
'set filepath as last known location in cell D54
Dim filepath as string
filepath = Sheet3.Range("D54").Value

If Dir(filepath) = "" Then
    MsgBox "File not found - check references"
    Exit Sub
End If

But it always returns a blank. I've tried every permutation I can think of: with and without square brackets around the filename; writing it manually into VBA rather than getting it from a cell value; tried nesting the Dir into a Len() function to check the string length instead; I've tried the Workbook.Open function using the same filename, both from a cell and written manually, and it works absolutely fine both ways. So it seems the filepath is valid - I don't understand what's going wrong with the Dir function.

Hope someone can help :)
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
The filepath stored from the last time the links were successfully established. Currently that is:

C:\Users\mrperson\Desktop\WLP system v2.4 (Sharepoint ready)\WLP\Template WLP 18-19.xlsm

(changed my username obviously) :)

The filepath is definitely valid, as it's used to establish the links to the external file. Dir just doesn't like it for some reason I can't fathom.
 
Upvote 0
I've managed to find a workaround: trying to open the target file and close without saving, and if the file can't be found in order to open, you can deal with that in the error handler to return the message:

Code:
Sub Auto_Open()

Application.ScreenUpdating = False
On Error Goto ErrHandler

'try to open & close WLP file object to check for valid filepath
Set TemplateWLP = Workbooks.Open(Sheet3.Range("D54").Value)
TemplateWLP.Close savechanges:=False




'if opened and closed without error, release the object and proceed with link refresh
Set TemplateWLP = Nothing
Call TemplateLinkProcess


Exit Sub


ErrHandler:


Set TemplateWLP = Nothing
MsgBox "Links to the Template WLP may be corrupt." & vbLf & vbLf & "Please re-establish the Template links using the button in the Staff Grid page before linking to any staff WLPs.", vbOKOnly


End Sub

It's a bit of a hack job but it works :) Sod Dir()
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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