
Originally Posted by
Norie
Why not just use Dir?
Sub TestForDir()
Dim strDir As String
strDir = "C:\My Documents\TestDir\"
If Dir(strDir, vbDirectory) = "" Then
MkDir strDir
Else
MsgBox "Directory exists."
End If
End Sub
Thank you so much for this elegant code! I registered just to reply because it has been SO helpful -- It has helped me extend a UDF by David Hager to test for specified path and add it if needed, all before opening said directory!
For those interested, here's how my code looks in combination with David Hager's.
Function ValidateHyperlink(Optional vFolderPath As String = "C:\")
'Trigger with changed value in another cell with this wording: "=B1&FollowHyperlink()"
'Code tests to see if the path exists. If not, it makes the directory before opening it. If it does, it opens it.
Dim strDir As String
strDir = vFolderPath
If Dir(strDir, vbDirectory) = "" Then
MkDir strDir
'Else
' MsgBox "Directory exists."
End If
On Error Resume Next
ThisWorkbook.FollowHyperlink [vFolderPath]
End Function