Hi All,
First post!
My question is (hopefully) very simple: I need a piece of code to add a new folder if the folder doesn't already exist.
I have a working code that renames folders that appear is column A to their new name in column E (I pulled from the internet). I want to simply add a line of code that creates a new folder for any rows in column E that were not already used to rename a folder. In my example below, I need my code to create a folder for Item # 3 ("3- I am a new folder name).
ub MakeFolders()
On Error Resume Next
Dim rowCounter As Integer, colCounter As Integer, totalRows As Integer, totalCols As Integer, charCounter As Integer, ICounter As Integer
Dim FolderPath As String, oldfolderPath As String, newfolderPath As String, folderRenamed As Long
Dim fDialog As FileDialog
Dim invalidChars ' array for invalid characters
invalidChars = Array("?", "<", ">", "/", "\", "|", ":", "*", "_")
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
If .Show = True Then
FolderPath = fDialog.SelectedItems(1)
subfolderPath = fDialog.SelectedItems(1)
Else
MsgBox "You didn't select any folder", vbInformation
FolderPath = vbNullString
End If
Worksheets("Rename_Folders").Range("G3") = FolderPath
End With
FolderPath = Sheets("Rename_Folders").Range("G3")
'No action if base folder not selelcted.
If FolderPath = vbNullString Then
MsgBox "Select Meeting Folder to Create Item Folders", vbInformation
Exit Sub
End If
'Get the total folders to rename
totalRows = WorksheetFunction.CountA(Worksheets("Rename_Folders").Range("A:A"))
'No action if folder name contains invalid characters.
For rowCounter = 2 To totalRows
For charCounter = 0 To 7
If InStr(Worksheets("Rename_Folders").Cells(rowCounter, 5), invalidChars(charCounter)) <> 0 Then
MsgBox "Invalid Character " & invalidChars(charCounter) & " found in " _
& Worksheets("Rename_Folders").Cells(rowCounter, 5).Address, vbError
Exit Sub
End If
Next
Next
'rename folders
folderRenamed = 0
For rowCounter = 2 To totalRows
oldfolderPath = FolderPath & "\" & Worksheets("Rename_Folders").Range("A" & rowCounter) 'old name
newfolderPath = FolderPath & "\" & Worksheets("Rename_Folders").Range("E" & rowCounter) 'new name
Exit Sub
'To Check if the old folder name exists or not
If Dir(oldfolderPath, vbDirectory) <> vbNullString Then
'Rename the Folder using Name function
Name oldfolderPath As newfolderPath
folderRenamed = folderRenamed + 1
Else
MsgBox "A New folder for" & vbNewLine & oldfolderPath & vbNewLine & "has been created .", vbInformation
End If
Next
If folderRenamed > 0 Then
MsgBox folderRenamed & " folders created successfully"
End If
Worksheets("Rename_Folders").Range("G3") = ""
FolderPath = vbNullString
End Sub
First post!
My question is (hopefully) very simple: I need a piece of code to add a new folder if the folder doesn't already exist.
I have a working code that renames folders that appear is column A to their new name in column E (I pulled from the internet). I want to simply add a line of code that creates a new folder for any rows in column E that were not already used to rename a folder. In my example below, I need my code to create a folder for Item # 3 ("3- I am a new folder name).
Column A | Column E | ||||||
|
| ||||||
2- Name that is less great existing folder v1 |
| ||||||
3- I am a new folder name |
ub MakeFolders()
On Error Resume Next
Dim rowCounter As Integer, colCounter As Integer, totalRows As Integer, totalCols As Integer, charCounter As Integer, ICounter As Integer
Dim FolderPath As String, oldfolderPath As String, newfolderPath As String, folderRenamed As Long
Dim fDialog As FileDialog
Dim invalidChars ' array for invalid characters
invalidChars = Array("?", "<", ">", "/", "\", "|", ":", "*", "_")
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
If .Show = True Then
FolderPath = fDialog.SelectedItems(1)
subfolderPath = fDialog.SelectedItems(1)
Else
MsgBox "You didn't select any folder", vbInformation
FolderPath = vbNullString
End If
Worksheets("Rename_Folders").Range("G3") = FolderPath
End With
FolderPath = Sheets("Rename_Folders").Range("G3")
'No action if base folder not selelcted.
If FolderPath = vbNullString Then
MsgBox "Select Meeting Folder to Create Item Folders", vbInformation
Exit Sub
End If
'Get the total folders to rename
totalRows = WorksheetFunction.CountA(Worksheets("Rename_Folders").Range("A:A"))
'No action if folder name contains invalid characters.
For rowCounter = 2 To totalRows
For charCounter = 0 To 7
If InStr(Worksheets("Rename_Folders").Cells(rowCounter, 5), invalidChars(charCounter)) <> 0 Then
MsgBox "Invalid Character " & invalidChars(charCounter) & " found in " _
& Worksheets("Rename_Folders").Cells(rowCounter, 5).Address, vbError
Exit Sub
End If
Next
Next
'rename folders
folderRenamed = 0
For rowCounter = 2 To totalRows
oldfolderPath = FolderPath & "\" & Worksheets("Rename_Folders").Range("A" & rowCounter) 'old name
newfolderPath = FolderPath & "\" & Worksheets("Rename_Folders").Range("E" & rowCounter) 'new name
Exit Sub
'To Check if the old folder name exists or not
If Dir(oldfolderPath, vbDirectory) <> vbNullString Then
'Rename the Folder using Name function
Name oldfolderPath As newfolderPath
folderRenamed = folderRenamed + 1
Else
MsgBox "A New folder for" & vbNewLine & oldfolderPath & vbNewLine & "has been created .", vbInformation
End If
Next
If folderRenamed > 0 Then
MsgBox folderRenamed & " folders created successfully"
End If
Worksheets("Rename_Folders").Range("G3") = ""
FolderPath = vbNullString
End Sub