Excel 如果名称相同,则替换文件夹
我想创建一个保存文件的文件夹。如果已经存在具有特定名称的文件夹,我想用新的空文件夹替换此文件夹。 我不知道怎么处理这件事。Im获取运行时错误75路径/文件访问错误Excel 如果名称相同,则替换文件夹,excel,vba,Excel,Vba,我想创建一个保存文件的文件夹。如果已经存在具有特定名称的文件夹,我想用新的空文件夹替换此文件夹。 我不知道怎么处理这件事。Im获取运行时错误75路径/文件访问错误 Path = "\\ASD\" d = Format(Date, "yyyymmdd") f = "NBT Reports " If Len(Dir(Path & d, vbDirectory)) = 0 Then MkDir (Path & f & d) ElseIf ??? End If 请尝试以下方法:
Path = "\\ASD\"
d = Format(Date, "yyyymmdd")
f = "NBT Reports "
If Len(Dir(Path & d, vbDirectory)) = 0 Then
MkDir (Path & f & d)
ElseIf ???
End If
请尝试以下方法:
Sub test()
Dim strPath As String
strPath = "\\ASD\"
Dim d As String
d = Format(Date, "yyyymmdd")
Dim f As String
f = "\NBT Reports"
Dim Folder As String
Folder = Dir(strPath & d, vbDirectory)
If Folder = vbNullString Then
Debug.Print "Doesnt Exist"
MkDir strPath
MkDir strPath & d
MkDir strPath & d & f
Else
Debug.Print "Exists"
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.deletefolder Left(strPath, Len(strPath) - 1)
If Dir(Left(strPath, Len(strPath) - 1)) = "" Then
MkDir strPath
MkDir strPath & d
MkDir strPath & d & f
End If
End If
End Sub
第二个嵌套IF语句出现,因为删除文件时oFSO似乎挂起,因此如果再次询问目录,它似乎会释放它,然后允许您再次创建目录