VBA检查文件是否存在

Jos*_*sta 35 excel vba file excel-vba

我有这个代码.它应该检查文件是否存在,如果存在则打开它.如果文件存在,它确实有效,但如果文件不存在,那么每当我将文本框留空并单击提交按钮时,它就会失败.我想要的是,如果文本框为空,则显示错误消息,就像文件不存在一样.

运行时错误"1004"

Dim File As String
File = TextBox1.Value
Dim DirFile As String

DirFile = "C:\Documents and Settings\Administrator\Desktop\" & File
If Dir(DirFile) = "" Then
  MsgBox "File does not exist"
Else
    Workbooks.Open Filename:=DirFile
End If
Run Code Online (Sandbox Code Playgroud)

bre*_*tdj 53

这样的事情

最好使用工作簿变量来提供打开的工作簿的进一步控制(如果需要)

更新以测试该文件名是一个实际工作簿 - 这也使初始检查冗余,而不是向用户发送消息而不是文本框为空

Dim strFile As String
Dim WB As Workbook
strFile = Trim(TextBox1.Value)
Dim DirFile As String
If Len(strFile) = 0 Then Exit Sub

DirFile = "C:\Documents and Settings\Administrator\Desktop\" & strFile
If Len(Dir(DirFile)) = 0 Then
  MsgBox "File does not exist"
Else
 On Error Resume Next
 Set WB = Workbooks.Open(DirFile)
 On Error GoTo 0
 If WB Is Nothing Then MsgBox DirFile & " is invalid", vbCritical
End If
Run Code Online (Sandbox Code Playgroud)


Pat*_*rez 33

我使用此函数来检查文件是否存在:

Function IsFile(ByVal fName As String) As Boolean
'Returns TRUE if the provided name points to an existing file.
'Returns FALSE if not existing, or if it's a folder
    On Error Resume Next
    IsFile = ((GetAttr(fName) And vbDirectory) <> vbDirectory)
End Function
Run Code Online (Sandbox Code Playgroud)

  • @ZygD然后你可以在`end function`之前添加一个`err.clear`.我个人总是在代码区之前清除错误,我会真正处理它们. (3认同)
  • 由于您有“On Error Resume Next”,在您的主线之后,我将引入“On Error GoTo 0”,以防止错误挂起。无论如何,我喜欢这种方法,因为可以检查文件的存在而不会意外检查文件夹的存在。 (2认同)
  • 进一步调查,似乎_GetAttr(fName)_将引发异常53 - FileNotFoundException,调用Resume Next,IsFile将保持其先前值(False).所以你的函数_does_处理所有情况.我可能不会测试它,但它也可能比brettdj运行得更快,因为它不会调用Dir,它看起来像系统命令(?).根据我的C/C++经验,调用系统命令大约需要1秒钟,也许还需要一秒钟才能恢复可执行文件.优秀!我之前提出了你的答案.我不明白为什么这不是最高票. (2认同)

Zyg*_*ygD 23

为了检查存在,还可以使用(适用于文件和文件夹):

Not Dir(DirFile, vbDirectory) = vbNullString
Run Code Online (Sandbox Code Playgroud)

结果是True存在文件或目录.

例:

If Not Dir("C:\Temp\test.xlsx", vbDirectory) = vbNullString Then
    MsgBox "exists"
Else
    MsgBox "does not exist"
End If
Run Code Online (Sandbox Code Playgroud)


Exc*_*ero 5

一种干净而简短的方法:

Public Function IsFile(s)
    IsFile = CreateObject("Scripting.FileSystemObject").FileExists(s)
End Function
Run Code Online (Sandbox Code Playgroud)


Jal*_*lal 5

Function FileExists(ByRef strFileName As String) As Boolean
' TRUE if the argument is an existing file
' works with Unicode file names
    On Error Resume Next
    Dim objFSO As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    FileExists = objFSO.FileExists(strFileName)
    On Error GoTo 0
End Function
Run Code Online (Sandbox Code Playgroud)

为了使函数运行得更快,可以将 objFSO 设为全局变量,然后修改代码并将其保存在模块中,如下所示:

Option Explicit
Dim objFSO As Object
Function FileExists(ByRef strFileName As String) As Boolean
' TRUE if the argument is an existing file
' works with Unicode file names
    On Error Resume Next
    If objFSO Is Nothing Then Set objFSO = CreateObject("Scripting.FileSystemObject")
    FileExists = objFSO.FileExists(strFileName)
    On Error GoTo 0
End Function
Run Code Online (Sandbox Code Playgroud)

为了strFileName成为 unicode 字符串,您可以从单元格值获取它或以特殊方式定义它,因为 Excel 的 VBE 不会以 Unicode 保存字符串常量。VBE 支持已保存在字符串变量中的 Unicode 字符串。您必须查找此内容以获取更多详细信息。

希望这对某人有帮助^_^