Excel VBA检查目录是否存在错误

use*_*463 38 directory excel vba file

我有一个电子表格,单击一个按钮将通过将所有内容复制/粘贴到新工作簿来复制自身,并使用依赖于某些变量值的名称保存文件(从电子表格中的单元格中获取).我目前的目标是让它将表单保存在不同的文件夹中,具体取决于客户端名称的名称(变量中保存的单元格值),而这在第一次运行时起作用,之后我收到错误.

代码检查目录是否存在,如果不存在则创建它.这可行,但在创建后,再次运行它会抛出错误:

运行时错误75 - 路径/文件访问错误.

我的代码:

Sub Pastefile()

Dim client As String
Dim site As String
Dim screeningdate As Date
screeningdate = Range("b7").Value
Dim screeningdate_text As String
screeningdate_text = Format$(screeningdate, "yyyy\-mm\-dd")
client = Range("B3").Value
site = Range("B23").Value

Dim SrceFile
Dim DestFile

If Dir("C:\2013 Recieved Schedules" & "\" & client) = Empty Then
    MkDir "C:\2013 Recieved Schedules" & "\" & client
End If

SrceFile = "C:\2013 Recieved Schedules\schedule template.xlsx"
DestFile = "C:\2013 Recieved Schedules\" & client & "\" & client & " " & site & " " & screeningdate_text & ".xlsx"

FileCopy SrceFile, DestFile

Range("A1:I37").Select
Selection.Copy
Workbooks.Open Filename:= _
    "C:\2013 Recieved Schedules\" & client & "\" & client & " " & site & " " & screeningdate_text & ".xlsx", UpdateLinks:= _
    0
Range("A1:I37").PasteSpecial Paste:=xlPasteValues
Range("C6").Select
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close

End Sub
Run Code Online (Sandbox Code Playgroud)

你不得不原谅我在这方面缺乏知识,我还在学习.我非常强烈地感觉它与目录检查逻辑有关,因为当抛出错误时,该MkDir行突出显示.

Bri*_*ire 102

要检查是否存在使用目录Dir,您需要指定vbDirectory第二个参数,如:

If Dir("C:\2013 Recieved Schedules" & "\" & client, vbDirectory) = "" Then
Run Code Online (Sandbox Code Playgroud)

需要注意的是,有vbDirectory,Dir就返回一个非空字符串,如果指定的路径已经存在的目录或文件(提供的文件不具有任何的只读,隐藏或系统属性).你可以GetAttr用来确定它是一个目录而不是一个文件.

  • 嗨布莱恩,非常感谢你的帮助.我认为这是比较非价值的逻辑问题.我很感激 :) (2认同)
  • 当路径包含 unicode 字符时失败(= 返回“”)。但是 ozmike 的解决方案有效。 (2认同)

ozm*_*ike 25

使用脚本对象的FolderExists方法.

Public Function dirExists(s_directory As String) As Boolean

Set OFSO = CreateObject("Scripting.FileSystemObject")
dirExists = OFSO.FolderExists(s_directory)

End Function
Run Code Online (Sandbox Code Playgroud)

  • 看起来这是一个非常一致的方法。对于那些采用使用“ Option Explicit”的最佳实践的人,我只会添加“ Dim OFSO As Object”。 (2认同)

Zyg*_*ygD 6

要确定文件夹是否存在(而不是文件),我使用此函数:

Public Function FolderExists(strFolderPath As String) As Boolean
    On Error Resume Next
    FolderExists = ((GetAttr(strFolderPath) And vbDirectory) = vbDirectory)
    On Error GoTo 0
End Function
Run Code Online (Sandbox Code Playgroud)

它既可以使用,也可以使用\.


Exc*_*ero 6

这是最干净的方式......到目前为止:

Public Function IsDir(s) As Boolean
    IsDir = CreateObject("Scripting.FileSystemObject").FolderExists(s)
End Function
Run Code Online (Sandbox Code Playgroud)


小智 5

If Len(Dir(ThisWorkbook.Path & "\YOUR_DIRECTORY", vbDirectory)) = 0 Then
   MkDir ThisWorkbook.Path & "\YOUR_DIRECTORY"
End If
Run Code Online (Sandbox Code Playgroud)


小智 5

我最终使用:

Function DirectoryExists(Directory As String) As Boolean
    DirectoryExists = False
    If Len(Dir(Directory, vbDirectory)) > 0 Then
        If (GetAttr(Directory) And vbDirectory) = vbDirectory Then
            DirectoryExists = True
        End If
    End If
End Function
Run Code Online (Sandbox Code Playgroud)

这是@Brian和@ZygD答案的混合。我认为On Error Resume Next@Brian的答案还不够,并且不喜欢@ZygD的答案中使用的答案