【问题标题】:Check if file exists works in VBA sub but not in VBA function despite calling the same function检查文件是否存在在 VBA 子中工作但在 VBA 函数中不工作尽管调用相同的函数
【发布时间】:2022-11-20 04:12:52
【问题描述】:

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

如果我通过运行宏来运行以下代码

Sub CheckFile()

  f = "I:\SomeFolder\a.txt"

  returnvalue = Dir(f)

  MsgBox (returnvalue)

End Sub

我在消息框中收到“a.txt”,因为文件存在!果然! 但是如果我写一个函数

Function FileExists(f)
    
    returnvalue = Dir(f)
    
    FileExists = returnvalue

End Function

并使用 =FileExists(A1),A1 的值为“I:\SomeFolder\a.txt”

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

相同的功能,相同的输入,不同的结果。我搞不清楚了。

【问题讨论】:

    标签: excel excel-formula


    【解决方案1】:

    傻我。我必须阅读变量的范围。在 FileExists 定义中将 (f) 更改为 (g) 就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多