Dir Function problem

Lochnagar

New Member
Joined
Jan 28, 2008
Messages
43
Hi,

I'm currently taking my first steps into using VBA to manipulate files, and have hit a stumbling block with the Dir function. The code below is a what I'm playing with,

Sub ffile()
ActiveCell = fileExists("fname")
End Sub

Function fileExists(fname) As String
fileExists = Dir(fname)
End Function


...basically, I understand that the Dir function (in this instance) returns the file name of the file from "fname" string if it exists, however, what I'm confused about is if you add ' <>"" ' to the end of the 'fileExists = Dir(fname)' line i.e. 'fileExists = Dir(fname) <>"" ' it returns True or False depending on whether the file exists or not.

I'm struggling to understand why this is the case and was hoping, if it is not too much trouble, if someone could help shed some light as to what this ' <>"" ' additional terms brings/means.

Any help will be hugely appreciated.

Thanks in advance,

Lochnagar
 
The Dir function returns the name of the file if it exists or "" if it doesn't exist. So the expression Dir(fname) <> "" returns True if the file exists, otherwise False. That result is being assigned to the variable fileExists.
 
Upvote 0

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