Using Dir() in a VBA function not working

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. Windows
I have what is probably a trivial question

In my sub, I have this code that tells me whether a file exists. It works.

VBA Code:
If Dir(sPath & sFileToProcessName, vbNormal) <> "" _
             Then

BUT I wanted to use a function instead of that code in my sub. It seems odd to me that when that same syntax used in my sub is used in the function I get an error #52, Bad file name or number.

Function call

VBA Code:
            If PathExists(sPath & sFileToProcessName, True) _
             Then

Function

VBA Code:
Function PathExists(psPath As String, Optional bFile As Boolean = False) As Boolean
    
    PathExists = False
    
Debug.Print "in function path = " & psPath
    
    If Right(psPath, 1) <> "\" Then psPath = psPath & "\"
    
    If bFile _
     Then
        If Dir(psPath, vbNormal) <> "" Then PathExists = True
    Else
        If Dir(psPath, vbDirectory) <> "" Then PathExists = True
    End If
    
End Function

What am I missing?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Your function is expecting a folder path, not a file name as well.
 
Upvote 0
I realize that my logic is flawed. If the "path" includes a FILE NAME my dumb code is adding a back slash to it. Now the function works as expected.

VBA Code:
Function PathExists(psPath As String, Optional bFile As Boolean = False) As Boolean
    
    PathExists = False
    
    If bFile _
     Then
        If Dir(psPath, vbNormal) <> "" Then PathExists = True
    Else
        If Right(psPath, 1) <> "\" Then psPath = psPath & "\"
        If Dir(psPath, vbDirectory) <> "" Then PathExists = True
    End If
    
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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