Check Existing File Failed

seageath

Board Regular
Joined
Oct 29, 2009
Messages
102
Hi ex(cel)perts.

I got the following code to check existing file to be opened.
But failed for one of the file on mapping drive
For example I want to check S:\Files\zTB Group.xls, it failed.
But on other file S:\Files\TB Group.xls it succeded
May I know why? It's been bugging me this whole day.

Code:
Function Control_IsFile_Exist(ByVal path As String)
    Dim value As Boolean
    
    If Dir$(path, vbDirectory) <> "" Then
        value = True
    ElseIf Dir$(path & ".xls", vbDirectory) <> "" Or Dir$(path & ".xlsx", vbDirectory) <> "" Or Dir$(path & ".xlsm", vbDirectory) <> "" Then
        value = True
    Else
        value = False
    End If
    Control_IsFile_Exist = value
End Function

Thanks in advance

-sea-
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Any idea what might cause the error?
Need your help urgently. Thanks a lot.

-sea-

Hi ex(cel)perts.

I got the following code to check existing file to be opened.
But failed for one of the file on mapping drive
For example I want to check S:\Files\zTB Group.xls, it failed.
But on other file S:\Files\TB Group.xls it succeded
May I know why? It's been bugging me this whole day.

Code:
Function Control_IsFile_Exist(ByVal path As String)
    Dim value As Boolean
    
    If Dir$(path, vbDirectory) <> "" Then
        value = True
    ElseIf Dir$(path & ".xls", vbDirectory) <> "" Or Dir$(path & ".xlsx", vbDirectory) <> "" Or Dir$(path & ".xlsm", vbDirectory) <> "" Then
        value = True
    Else
        value = False
    End If
    Control_IsFile_Exist = value
End Function

Thanks in advance

-sea-
 
Upvote 0
Please could you expand on what actually happens ie do you get a VBA error? Or do you mean you get an unexpected result (eg because you think the file exists and the function returns False)? If an unexpected result, have you used F8 within the VBE to step through the code line by line to identify why the code may not be returning the expected result?
 
Upvote 0
Try this
Code:
Function Control_IsFile_Exist(ByVal path As String)
    Dim value As Boolean
    
    If Dir$(path, vbDirectory) <> "" Or Dir$(path & ".xls", vbDirectory) <> "" Or Dir$(path & ".xlsx", vbDirectory) <> "" Or Dir$(path & ".xlsm", vbDirectory) <> "" Then
        
        value = True
    Else
        value = False
    End If
    Control_IsFile_Exist = value
End Function

please avoid using words like Value, Date, Time in variables it confuses VBA
 
Last edited:
Upvote 0
Case Closed - Check Existing File Failed

Hi all, sorry for the "silly question" :eeek:

Just found out there is a "space" after ".xls" therefore I just put Trim function for the value and everything working just fine. Thanks for all the help

:biggrin:

-sea-
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,856
Members
452,948
Latest member
UsmanAli786

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