检查文件是否存在在VBA子中工作,但在VBA函数中不工作,尽管调用相同的函数



我正在尝试检查是否存在一个文件

如果我通过运行宏

运行以下代码
Sub CheckFile()
f = "I:SomeFoldera.txt"
returnvalue = Dir(f)
MsgBox (returnvalue)
End Sub

我得到"a.txt"在消息框中,因为文件存在!应该!但是如果我写一个函数

Function FileExists(f)

returnvalue = Dir(f)

FileExists = returnvalue
End Function

and use =FileExists(A1),其中A1的值为"I:SomeFoldera.txt">

得到0 ?????????

相同的功能,相同的输入,不同的结果。我迷路了。

我真傻。我必须读取变量的作用域。将FileExists定义中的(f)改为(g)就可以了。

最新更新