关于,一般有如下的两种" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>excel</em> <em>vba</em>判断文件是否存在-

<em>excel</em> <em>vba</em>判断文件是否存在-

作者:乔山办公网日期:

返回目录:excel表格制作


文件不能反复打开,你上次打开后没有关闭,再次打开就会这样,可以关闭EXCEL后重新打开。

关于,一般有如下的两种方式。 请根据情况,选择一种适合你的方法。 一、通过FileExists判断文件是否存在 Dim MyFile As Object Set MyFile = CreateObject("Scripting.FileSystemObject") If MyFile.FileExists("E:\dzwebs\Pic\logo.gif" ) = True Then MsgBox "文件存在" Else MsgBox "文件不存在" End If 以上代码在VBA环境下使用。或者将其制作成为宏也可以。"E:\dzwebs\Pic\logo.gif"为文件路径及文件名称,可自行修改。 二、通过Dir来判断 Dim YouFile As String YouFile = Dir("E:\MyPictures\Pic\logo.gif") If x <> "" Then MsgBox "文件存在" Else MsgBox "文件不存在" Endif "E:\MyPictures\Pic\logo.gif"为文件路径及文件名称,可自行修改。当然,如上代码,也在VBA下使用,可自作成为宏或函数都可以。 上面的两种方法,本站推荐你使用第一种方法,因e799bee5baa6e997aee7ad94e4b893e5b19e339为第一种方法比较符合逻辑思维。
Const fName As String = "MyExcelFile.xls"
Sub testFileOpen()
Dim xApp As Application
Set xApp = CreateObject("Excel.Application")
xApp.DisplayAlerts = False
On Error GoTo FileError
xApp.Workbooks.Open Filename:=ThisWorkbook.Path & "\" & fName, notify:=False, ReadOnly:=False
If xApp.ActiveWorkbook.ReadOnly = True Then
    MsgBox "File is already opened!"
Else
    MsgBox "File is not opened!"
End If
GoTo QuitSub
FileError:
    MsgBox "File is not exist"
QuitSub:
    xApp.Quit
End Sub

附件可下载参考



先查找这个文件是否存在,如果存在,删除,再移动不就行了

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网