问题描述
好吧,所以我有以下vba代码,用于检查目录是否存在,如果不存在,则创建文件夹结构,如下所示:
Ok so I have the following vba code which I am using to check if a directory exists and if not create the folder structure like so:
If Dir("S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\" & Range("M" & ActiveCell.Row).Value & "\" & Range("Z" & ActiveCell.Row).Value, vbDirectory) = "" Then
MkDir Path:="S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\" & Range("M" & ActiveCell.Row).Value & "\" & Range("Z" & ActiveCell.Row).Value
MsgBox "Done"
Else
MsgBox "found it"
End If
所以我的目标路径是我的 S:\
驱动器
So my destination path is my S:\
drive
然后根据单元格c中的值,我希望它检查该文件夹是否存在,因此,如果单元格c中包含单词"tender",则目录如下所示:
then depending on the value in cell c I want it to check if that folder exists, so if cell c had the word 'tender' in it then the directory would look like:
'S:\Tender'
如果不存在,则创建,否则,继续并在该文件夹中创建另一个文件夹,并在其中保存单元格M中的值,如下所示:
If this does not exist, then create, else if this exists then move on and create another folder within this folder with the value in cell M like so:
Cell M = Telecoms
'S:\Tender\Telecoms'
然后,最后检查'S:\ Tender \ Telecoms'中是否存在具有Z单元格中值的文件夹,如果没有,则创建它.
Then finally, check if a folder with the value in cell Z exists within 'S:\Tender\Telecoms' and if not create it.
Cell Z = 12345
所以我们最终会得到:
'S:\Tender\Telecoms\12345\'
由于某些原因,我一直找不到错误消息路径.请有人能告诉我我要去哪里错吗?预先感谢
Fore some reason I keep getting the error message path not found. Please can someone show me where I am going wrong? Thanks in advance
推荐答案
我不久前写了我保存在库中的小东西:
I wrote some time ago this little thing that I keep in my library:
Function CreateFolder(ByVal sPath As String) As Boolean
'by Patrick Honorez - www.idevlop.com
'create full sPath at once, if required
'returns False if folder does not exist and could NOT be created, True otherwise
'sample usage: If CreateFolder("C:\toto\test\test") Then debug.print "OK"
'updated 20130422 to handle UNC paths correctly ("\\MyServer\MyShare\MyFolder")
Dim fs As Object
Dim FolderArray
Dim Folder As String, i As Integer, sShare As String
If Right(sPath, 1) = "\" Then sPath = Left(sPath, Len(sPath) - 1)
Set fs = CreateObject("Scripting.FileSystemObject")
'UNC path ? change 3 "\" into 3 "@"
If sPath Like "\\*\*" Then
sPath = Replace(sPath, "\", "@", 1, 3)
End If
'now split
FolderArray = Split(sPath, "\")
'then set back the @ into \ in item 0 of array
FolderArray(0) = Replace(FolderArray(0), "@", "\", 1, 3)
On Error GoTo hell
'start from root to end, creating what needs to be
For i = 0 To UBound(FolderArray) Step 1
Folder = Folder & FolderArray(i) & "\"
If Not fs.FolderExists(Folder) Then
fs.CreateFolder (Folder)
End If
Next
CreateFolder = True
hell:
End Function
这篇关于VBA检查目录是否存在,如果存在退出子,否则不存在,创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!