Pinaceous
Well-known Member
- Joined
- Jun 11, 2014
- Messages
- 1,113
- Office Version
- 365
- Platform
- Windows
Hi All,
I'm working with a code that crashes to the point where I have to use the task manager to shut down Excel after it has run.
The code creates a folder with sub folders based upon cell designation(s).
For example, on Worksheet(1) Column A Cell A1 its contents creates the main folder with sub folders based on Column B Cells B1:B10 based upon its names.
So this screen shot in running the code would create the Main Folder titled "Folder A" with its sub folders inside of it called "Sub B1" … to ... "Sub B10".
It seems easy enough but I cannot get it straight. Here is the code, that I don't advise running unless you see how it can be corrected.
Thank you,
pinaceous
I'm working with a code that crashes to the point where I have to use the task manager to shut down Excel after it has run.
The code creates a folder with sub folders based upon cell designation(s).
For example, on Worksheet(1) Column A Cell A1 its contents creates the main folder with sub folders based on Column B Cells B1:B10 based upon its names.
So this screen shot in running the code would create the Main Folder titled "Folder A" with its sub folders inside of it called "Sub B1" … to ... "Sub B10".
It seems easy enough but I cannot get it straight. Here is the code, that I don't advise running unless you see how it can be corrected.
VBA Code:
Sub CreateFolders()
Dim aCustomers
Dim aArticles
Dim i
Dim j
Dim sPath
sPath = "C:\Users\Desktop\"
With ThisWorkbook.Sheets(1)
aCustomers = .Range(.Range("A1"), .Range("A1").End(xlDown)).Value
aArticles = .Range("B1:B10").Value
End With
For i = LBound(aCustomers, 1) To UBound(aCustomers, 1)
For j = LBound(aArticles, 1) To UBound(aArticles, 1)
SmartCreateFolder sPath & "\" & aCustomers(i, 1) & "\" & aArticles(j, 1)
Next
Next
End Sub
Sub SmartCreateFolder(sFolder)
Static oFSO As Object
If oFSO Is Nothing Then Set oFSO = CreateObject("Scripting.FileSystemObject")
With oFSO
If Not .FolderExists(sFolder) Then
SmartCreateFolder .GetParentFolderName(sFolder)
.CreateFolder sFolder
End If
End With
End Sub
Thank you,
pinaceous