Here's what I use:
Sub Demo()
MakeDirs Intersect(Columns("A"), ActiveSheet.UsedRange).Value
End Sub
Sub MakeDirs(avsDir As Variant)
' shg 2008
Dim vsDir As Variant
For Each vsDir In avsDir
vsDir = CStr(vsDir)
If Len(vsDir) Then
Select Case MakeDir(vsDir)
Case -1
MsgBox Prompt:="MakeDirs: Directory """ & vsDir & """ already exists", _
Title:="Oops!"
Case 0
MsgBox Prompt:="MakeDirs: Unable to create """ & vsDir & """", _
Title:="Oops!"
End Select
End If
Next vsDir
End Sub
Function MakeDir(ByVal sDir As String) As Long
' shg 2008
' Returns -1 if sDir already exists
' 1 if sDir is successfully created
' 0 if sDir cannot be created
Dim sPS As String
Dim astr() As String
Dim iLvl As Long
If Len(Dir(sDir, vbDirectory)) Then
MakeDir = -1
Else
sPS = Application.PathSeparator
If Right(sDir, 1) = sPS Then sDir = Left(sDir, Len(sDir) - 1)
astr = Split(sDir, sPS)
sDir = vbNullString
' MkDir will fail if sDir contains character codes 0-31
' or any of the characters ' < > : " / \ | ? *
' or if the drive does not exist,
' or if the user does not have permissions, so ...
On Error GoTo Oops
For iLvl = 0 To UBound(astr)
sDir = sDir & astr(iLvl) & sPS
If Len(Dir(sDir, vbDirectory)) = 0 Then MkDir sDir
Next iLvl
MakeDir = 1
End If
OuttaHere:
Exit Function
Oops:
Err.Clear
Resume OuttaHere
End Function
Bookmarks