Hi everyone,
I am trying to create a VBA Code where:
1) Every time you insert a text into a cell in column A, a new folder is created
2) Every time a cell in column A is deleted, the folder with the same name is also deleted
I copied a VBA code online that made it so that the first condition is true. However, after the code is run once and adds the initial list of folders, any subsequent additions to column A won't create new folders unless I delete the initial list of folders. I have tried changing the condition, "If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0", to try and work around this issue but to no avail.
Is there also VBA code that helps me to dynamically delete folders with the same name as my cell values or refreshes the folders I have created to be the same as the list of names in column A?
Apologies if any of what I said sounds confusing. I am not very experienced in VBA Coding. Thank you for the help.
I am trying to create a VBA Code where:
1) Every time you insert a text into a cell in column A, a new folder is created
2) Every time a cell in column A is deleted, the folder with the same name is also deleted
I copied a VBA code online that made it so that the first condition is true. However, after the code is run once and adds the initial list of folders, any subsequent additions to column A won't create new folders unless I delete the initial list of folders. I have tried changing the condition, "If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0", to try and work around this issue but to no avail.
Is there also VBA code that helps me to dynamically delete folders with the same name as my cell values or refreshes the folders I have created to be the same as the list of names in column A?
Apologies if any of what I said sounds confusing. I am not very experienced in VBA Coding. Thank you for the help.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Z As Long
Dim xVal As String
On Error Resume Next
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Z = 1 To Target.Count
If Target(Z).Value > 0 Then
Call MakeFolders
End If
Next
Application.EnableEvents = True
End Sub
Sub MakeFolders()
Dim Rng As Range
Dim LastRow As Long
Dim LastColumn As Long
Dim maxRows As Integer
Dim r As Integer
Dim c As Integer
Set sht = Worksheets("Activity List Data")
Set StartCell = Range("A2")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = 1
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
Set Rng = Selection
maxRows = Rng.Rows.Count
For c = 1 To 1
r = 1
Do While r <= maxRows
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))
On Error Resume Next
End If
r = r + 1
Loop
Next c
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
End Sub