Private Sub subCreateFoldersVersionThree()
Dim i As Integer
Dim ii As Integer
Dim iii As Integer
Dim varIndex As Variant
Dim arr() As String
Dim fdObj As Object
Dim strFormula As String
Dim Q As String
Dim strBaseFolder As String
Dim arrForbidden() As String
Dim strForbidden As String
Dim blnfail As Boolean
Dim strCreate As String
Dim strMsg As String
Dim rngFolders As Range
Dim intCount As Integer
On Error GoTo Err_Handler
ActiveWorkbook.Save
Application.Calculation = xlCalculationAutomatic
If MsgBox("Are your folders in this worksheet?", vbYesNo, "Question") = vbNo Then
Exit Sub
End If
Q = Chr(34)
strForbidden = "< > : / \ ? | * " & Q
arrForbidden = Split(strForbidden, " ")
Set fdObj = CreateObject("Scripting.FileSystemObject")
strBaseFolder = "C:\abcdef\"
If Not fdObj.FolderExists(strBaseFolder) Then
MsgBox "Base folder, '" & strBaseFolder & "' does not exist.", vbCritical, "Warning!"
Exit Sub
End If
strFormula = "=SUBSTITUTE(LEFT(SUBSTITUTE($A1," & Q & ".0" & Q & "," & Q & Q & "),FIND(" & Q & " " & _
Q & ",SUBSTITUTE($A1," & Q & ".0" & Q & "," & Q & Q & "),1)-1)," & Q & "." & Q & "," & Q & Q & ")"
Range("A1").CurrentRegion.Columns(1).Offset(0, 1).Formula = strFormula
Set rngFolders = Range("B1:B" & ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row)
strFormula = "=(IF(LEN($B1)>1,MATCH(LEFT(SUBSTITUTE(LEFT($A1,FIND(" & Q & " " & Q & ",$A1,1)-1)," & Q & "." & Q & "," & Q & Q & ")," & _
"LEN(SUBSTITUTE(LEFT($A1,FIND(" & Q & " " & Q & ",$A1,1)-1)," & Q & "." & Q & "," & Q & Q & "))-1)," & rngFolders.Address & ",0)," & Q & Q & "))"
Range("A1").CurrentRegion.Columns(1).Offset(0, 2).Formula = strFormula
arrFlds = Range("A1:C" & ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row)
For i = LBound(arrFlds) To UBound(arrFlds)
For iii = LBound(arrForbidden) To UBound(arrForbidden)
If InStr(1, arrFlds(i, 1), arrForbidden(iii), vbTextCompare) > 0 Then
strMsg = "This folder name has a '" & arrForbidden(iii) & "' invalid character in it."
strMsg = strMsg & vbCrLf & vbCrLf & arrFlds(i, 1)
MsgBox strMsg, vbCritical, "Warning!"
Exit Sub
End If
Next iii
Next i
Range("A1").CurrentRegion.Columns(2).ClearContents
Range("A1").CurrentRegion.Columns(3).ClearContents
For i = UBound(arrFlds) To 1 Step -1
strPath = arrFlds(i, 1)
If arrFlds(i, 3) <> "" Then
varIndex = arrFlds(i, 3)
If varIndex <> "" Then
Do While varIndex <> ""
strPath = arrFlds(varIndex, 1) & "\" & strPath
varIndex = arrFlds(varIndex, 3)
Loop
End If
arr = Split(strPath, "\")
strPath = ""
For ii = LBound(arr) To UBound(arr)
strPath = strPath & "\" & arr(ii)
strCreate = Replace(strBaseFolder & strPath, "\\", "\", 1)
If Not fdObj.FolderExists(strCreate) Then
fdObj.CreateFolder (strCreate)
intCount = intCount + 1
End If
Next ii
End If
Next i
strCreate = ""
If intCount = 0 Then
strMsg = "No folders have been created."
strMsg = strMsg & vbCrLf & "This may be because they already existed."
Else
strMsg = intCount & " folders have been created."
End If
MsgBox strMsg, vbInformation, "Confirmation."
Exit_Handler:
Set fdObj = Nothing
Exit Sub
Err_Handler:
MsgBox "There has been an error. Not all of the folders will have been created", vbCritical, "Warning!"
MsgBox "The problem may be with the following path." & vbCrLf & strCreate, vbInformation, "Information"
Resume Exit_Handler
End Sub