VBA检查文件是否存在

我有这个代码。 它应该检查一个文件是否存在,如果有的话打开它。 如果文件存在,它会起作用,如果不存在,那么每当我将文本框留空并单击提交button时,它就会失败。 我想要的,如果文本框是空的就显示错误消息,就像文件不存在一样。

运行时错误“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 

像这样的东西

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

更新来testing该文件名是一个实际的工作簿 – 这也使得最初的检查冗余,除了消息的用户比文本框是空白

 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 

使用此function来检查文件是否存在:

 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 

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

 Not Dir(DirFile, vbDirectory) = vbNullString 

如果存在文件或目录,则结果为True

例:

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

也许它是由Filenamevariables引起的

 File = TextBox1.Value 

它应该是

 Filename = TextBox1.Value 

我会把它扔到那里然后鸭子。 检查文件是否存在的常见原因是在尝试打开文件时避免出现错误。 如何使用error handling程序来处理:

 Function openFileTest(filePathName As String, ByRef wkBook As Workbook, _ errorHandlingMethod As Long) As Boolean 'Returns True if filePathName is successfully opened, ' False otherwise. Dim errorNum As Long '*************************************************************************** ' Open the file or determine that it doesn't exist. On Error Resume Next: Set wkBook = Workbooks.Open(fileName:=filePathName) If Err.Number <> 0 Then errorNum = Err.Number 'Error while attempting to open the file. Maybe it doesn't exist? If Err.Number = 1004 Then '*************************************************************************** 'File doesn't exist. 'Better clear the error and point to the error handler before moving on. Err.Clear On Error GoTo OPENFILETEST_FAIL: '[Clever code here to cope with non-existant file] '... 'If the problem could not be resolved, invoke the error handler. Err.Raise errorNum Else 'No idea what the error is, but it's not due to a non-existant file 'Invoke the error handler. Err.Clear On Error GoTo OPENFILETEST_FAIL: Err.Raise errorNum End If End If 'Either the file was successfully opened or the problem was resolved. openFileTest = True Exit Function OPENFILETEST_FAIL: errorNum = Err.Number 'Presumabley the problem is not a non-existant file, so it's 'some other error. Not sure what this would be, so... If errorHandlingMethod < 2 Then 'The easy out is to clear the error, reset to the default error handler, 'and raise the error number again. 'This will immediately cause the code to terminate with VBA's standard 'run time error Message box: errorNum = Err.Number Err.Clear On Error GoTo 0 Err.Raise errorNum Exit Function ElseIf errorHandlingMethod = 2 Then 'Easier debugging, generate a more informative message box, then terminate: MsgBox "" _ & "Error while opening workbook." _ & "PathName: " & filePathName & vbCrLf _ & "Error " & errorNum & ": " & Err.Description & vbCrLf _ , vbExclamation _ , "Failure in function OpenFile(), IO Module" End Else 'The calling function is ok with a false result. That is the point 'of returning a boolean, after all. openFileTest = False Exit Function End If End Function 'openFileTest() 

你应该设置一个条件循环来检查TextBox1的值。

 If TextBox1.value = "" then MsgBox "The file not exist" Exit sub 'exit the macro End If 

希望它能帮助你。