vba check if directory exists, if exists exit sub else if does not exist, create

徘徊边缘 提交于 2021-02-17 05:21:13

问题


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

So my destination path is my S:\ drive

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'

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'

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

so we would end up with:

'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


回答1:


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



回答2:


Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal DirPath As String) As Long
MakeSureDirectoryPathExists "S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\" & Range("M" & ActiveCell.Row).Value & "\" & Range("Z" & ActiveCell.Row).Value



回答3:


The MkDir command is only going to create a single new level of subdirectory.

Sub directory()
    Dim rw As Long, f As String

    rw = ActiveCell.Row
    f = "s:\Tasks"
    If Not CBool(Len(Dir(f, vbDirectory))) Then
        MkDir Path:=f
        Debug.Print "made " & f
    End If
    f = f & Chr(92) & Range("C" & rw).Value
    If Not CBool(Len(Dir(f, vbDirectory))) Then
        MkDir Path:=f
        Debug.Print "made " & f
    End If
    f = f & Chr(92) & Range("M" & rw).Value
    If Not CBool(Len(Dir(f, vbDirectory))) Then
        MkDir Path:=f
        Debug.Print "made " & f
    End If
    f = f & Chr(92) & Range("Z" & rw).Value
    If Not CBool(Len(Dir(f, vbDirectory))) Then
        MkDir Path:=f
        Debug.Print "made " & f
    Else
        Debug.Print "it was already there"
    End If

End Sub


来源:https://stackoverflow.com/questions/32474451/vba-check-if-directory-exists-if-exists-exit-sub-else-if-does-not-exist-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!