如何使用VBA Excel 2007确定文件是否存在? [英] How Do I Determine If File Exists Using VBA Excel 2007?

查看:96
本文介绍了如何使用VBA Excel 2007确定文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重写使用FileSearch for Excel 2003 VBA的一些代码。我试图调用一个应该确定1或0并使用的函数。如果语句我将执行一些代码或迭代到下一个文件。

I am attempting to rewrite some code that was using FileSearch for Excel 2003 VBA. I'm attempting to call a function that should determine 1 or 0 and using an If statement I will execute some code or iterate to the next file.

我没有从我的功能返回正确的结果。

I am not returning the correct result from my function.

我的代码:

 Dim MyDir As String, Fn As String
 Dim MyFile As String

   MyDir = "C:Test\"
   Fn = "" & "" & Examiner & " " & MnName & " " & Yr & ".xls"
   MyFile = MyDir & """" & Fn & """"

    If FileThere(MyFile) Then
    MsgBox yes

    Else
    MsgBox Not there

    End If

    '''''''''''''''''
    Function FileThere(FileName As String) As Boolean
         FileThere = (Dir(FileName) > "")
    End Function


推荐答案

Sub a()

MsgBox "test1 " & FileThere("c:\test1.bat")
MsgBox "k1" & FileThere("c:\k1")

End Sub

Function FileThere(FileName As String) As Boolean
     If (Dir(FileName) = "") Then
        FileThere = False
     Else:
        FileThere = True
     End If
End Function

这篇关于如何使用VBA Excel 2007确定文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆