jeran042
Registered User.
- Local time
- Today, 13:11
- Joined
- Jun 26, 2017
- Messages
- 127
Good morning all,
I have a command button on a form that will create a new subfolder within each folder in a specified directory. This code runs fine, but I would like any input if this is the best way to do this, or if I am missing something crucial.
Here is what I have for code:
The basis for this code came from "Barb Reinhardt" from:
http://answers.microsoft.com/en-us/msoffice/forum/msoffice_other-msoffice_custom-mso_2010/create-subfolders-using-vba/39419be9-60d1-4fd1-8b3c-e3ca0eaf54c2
Any input would be well appreciated!
I have a command button on a form that will create a new subfolder within each folder in a specified directory. This code runs fine, but I would like any input if this is the best way to do this, or if I am missing something crucial.
Here is what I have for code:
Code:
Private Sub Command28_Click()
Dim RootFolder As Object
Dim SubFolder As Object
Dim myFolder As String
Dim myNewFolder As String
Dim mySubfolderPath As String
Dim myFile As String
Dim sFolderName As String
Set fso = CreateObject("Scripting.FileSystemObject")
'User input box to get the desired new folder name
sFolderName = Trim(UCase(InputBox("Enter Folder Name:", "CREATE NEW FOLDER")))
'Test if "Cancel" button was pushed
If sFolderName = "" Then Exit Sub
'Confirm new folder name
If MsgBox("Folder Name: " & sFolderName, vbInformation + vbYesNo, "CONFIRM FOLDER NAME") = vbNo Then Exit Sub
'Change to identify your main folder - MAKE SURE TO HAVE THE TRAILING "\"
myFolder = "C:\Users\jrenald\Desktop\TEST\"
Set RootFolder = fso.GetFolder(myFolder)
'Loop through all subfolders in parent directory
For Each SubFolder In RootFolder.SubFolders
mySubfolderPath = SubFolder.path
myNewFolder = mySubfolderPath & "\" & sFolderName
'Debug.Print SubFolder.path & "\" & sFolderName
If Not fso.FolderExists(myNewFolder) Then
MkDir (myNewFolder)
Else
MsgBox "The Folder Name: " & "'" & sFolderName & "'" & " Already Exists!" _
& vbNewLine & "Please use that folder, or create a new one", vbCritical, "DUPLICATE NAME WARNING"
Exit Sub
End If
Next SubFolder
'Conformation message that folders have been created
MsgBox "Your New Folder: " & sFolderName _
& vbNewLine & "Has been added to the following Directory of Subfolders: " _
& myFolder, vbInformation, "SUCCESS!"
End Sub
The basis for this code came from "Barb Reinhardt" from:
http://answers.microsoft.com/en-us/msoffice/forum/msoffice_other-msoffice_custom-mso_2010/create-subfolders-using-vba/39419be9-60d1-4fd1-8b3c-e3ca0eaf54c2
Any input would be well appreciated!