Sure, I understand that you want the data listed hierarchically with each folder and its contents, including subfolders, and have folders and subfolders’ names in bold font. Let’s adjust the code accordingly.
Part 1: List Contents Hierarchically
Here’s the function to list out the folder contents hierarchically, with folder and subfolder names in bold:
Function CreateOrRetrieveSheet(sheetName As String) As Worksheet
On Error Resume Next ’ Ignore errors while attempting to get sheet
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0 ’ Turn error handling back on
code
If CreateOrRetrieveSheet Is Nothing Then
’ Sheet does not exist, create it
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets.Add
CreateOrRetrieveSheet.Name = sheetName
Else
’ Clear existing content
CreateOrRetrieveSheet.Cells.Clear
End If
End Function
Sub ListFolderContentToSheet()
Dim ws As Worksheet
Set ws = CreateOrRetrieveSheet(“LISTA”)
code
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = “Select a Folder”
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
sItem = .SelectedItems(1)
End With
ws.Cells(1, 4).Value = sItem ’ Store the folder path in cell D1
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical sItem, ws, rowNum, 0
End Sub
Sub GetFolderContentHierarchical(ByVal FolderPath As String, ByVal ws As Worksheet, ByRef rowNum As Long, ByVal level As Integer)
Dim FSO As Object
Dim ParentFolder As Object
Dim FolderItem As Object
Dim FileItem As Object
Dim currentRow As Long
code
Assistant
User
ALSO COLOUR OF CELLS MUST REMAIN UNCHAIGED IN SHEET LISTA WHEN WE MAKE UPDATE OR REFRESH
Assistant
User
WHERE I MUST PUT THIS CODE?
Assistant
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Set ParentFolder = FSO.GetFolder(FolderPath)
’ List the folder itself
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & ParentFolder.Path & “”
ws.Cells(rowNum, 1).Font.Bold = True
rowNum = rowNum + 1
’ List the items in the folder
For Each FolderItem In ParentFolder.SubFolders
GetFolderContentHierarchical FolderItem.Path, ws, rowNum, level + 1
Next FolderItem
For Each FileItem In ParentFolder.Files
If Right(FileItem.Name, 4) <> “.tmp” Then
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & FileItem.Path
ws.Cells(rowNum, 2).Value = FileItem.DateLastModified
rowNum = rowNum + 1
End If
Next FileItem
End Sub
Part 2: Verify Changes and Update the LISTA Sheet and List Changes in MODIFICARI
Here’s the code that checks for changes and updates LISTA, and lists the changes in MODIFICARI:
Sub MonitorFolderAndUpdateChanges()
Dim wsLISTA As Worksheet
Dim wsChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
code
Set wsLISTA = ThisWorkbook.Sheets(“LISTA”)
Set wsChanges = CreateOrRetrieveSheet(“MODIFICARI”)
Set dictOriginal = CreateObject(“Scripting.Dictionary”)
Set dictCurrent = CreateObject(“Scripting.Dictionary”)
wsChanges.Cells.Clear
’ Get folder path from sheet LISTA cell D1
folderPath = wsLISTA.Cells(1, 4).Value
If folderPath = “” Then
MsgBox “No folder path found in LISTA! Please run the listing first.”, vbExclamation
Exit Sub
End If
’ Populate dictionary with initial listing
For Each cell In wsLISTA.Range(“A1:A” & wsLISTA.Cells(wsLISTA.Rows.Count, “A”).End(xlUp).Row)
If cell.Value <> “” And Right(cell.Value, 1) <> “” Then
dictOriginal(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
’ List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet(“TempListing”)
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical folderPath, wsTemp, rowNum, 0
’ Populate dictionary with current listing
For Each cell In wsTemp.Range(“A1:A” & wsTemp.Cells(wsTemp.Rows.Count, “A”).End(xlUp).Row)
If cell.Value <> “” And Right(cell.Value, 1) <> “” Then
dictCurrent(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
Dim rowChanges As Long
rowChanges = 1
wsChanges.Cells(rowChanges, 1).Value = “Path”
wsChanges.Cells(rowChanges, 2).Value = “Change Type”
wsChanges.Cells(rowChanges, 3).Value = “Last Modified Date”
’ Find deleted files/folders
For Each key In dictOriginal.Keys
If Not dictCurrent.Exists(key) Then
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = “Deleted”
wsChanges.Cells(rowChanges, 3).Value = dictOriginal(key)
End If
Next key
’ Find added and modified files/folders
For Each key In dictCurrent.Keys
If Not dictOriginal.Exists(key) Then ’ New file/folder
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = “Added”
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
ElseIf dictCurrent(key) <> dictOriginal(key) Then ’ Modified
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = “Modified”
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
End If
Next key
’ Update LISTA sheet with current data
wsLISTA.Cells.Clear
wsLISTA.Cells(1, 4).Value = folderPath
wsTemp.UsedRange.Copy wsLISTA.Cells(1, 1)
’ Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Complete Code with Hierarchical Listing and Change Monitoring
Here is the complete VBA code for both listing contents and monitoring changes:
Function CreateOrRetrieveSheet(sheetName As String) As Worksheet
On Error Resume Next ’ Ignore errors while attempting to get sheet
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0 ’ Turn error handling back on
code
If CreateOrRetrieveSheet Is Nothing Then
’ Sheet does not exist, create it
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets.Add
CreateOrRetrieveSheet.Name = sheetName
Else
’ Clear existing content
CreateOrRetrieveSheet.Cells.Clear
End If
End Function
Sub ListFolderContentToSheet()
Dim ws As Worksheet
Set ws = CreateOrRetrieveSheet(“LISTA”)
code
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = “Select a Folder”
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
sItem = .SelectedItems(1)
End With
ws.Cells(1, 4).Value = sItem ’ Store the folder path in cell D1
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical sItem, ws, rowNum, 0
End Sub
Sub GetFolderContentHierarchical(ByVal FolderPath As String, ByVal ws As Worksheet, ByRef rowNum As Long, ByVal level As Integer)
Dim FSO As Object
Dim ParentFolder As Object
Dim FolderItem As Object
Dim FileItem As Object
Dim currentRow As Long
code
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Set ParentFolder = FSO.GetFolder(FolderPath)
’ List the folder itself
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & ParentFolder.Path & “”
ws.Cells(rowNum, 1).Font.Bold = True
rowNum = rowNum + 1
’ List the items in the folder
For Each FolderItem In ParentFolder.SubFolders
GetFolderContentHierarchical FolderItem.Path, ws, rowNum, level + 1
Next FolderItem
For Each FileItem In ParentFolder.Files
If Right(FileItem.Name, 4) <> “.tmp” Then
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & FileItem.Path
ws.Cells(rowNum, 2).Value = FileItem.DateLastModified
rowNum = rowNum + 1
End If
Next FileItem
End Sub
Sub MonitorFolderAndUpdateChanges()
Dim wsLISTA As Worksheet
Dim wsChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
code
Set wsLISTA = ThisWorkbook.Sheets(“LISTA”)
Set wsChanges = CreateOrRetrieveSheet(“MODIFICARI”)
Set dictOriginal = CreateObject(“Scripting.Dictionary”)
Set dictCurrent = CreateObject(“Scripting.Dictionary”)
wsChanges.Cells.Clear
’ Get folder path from sheet LISTA cell D1
folderPath = wsLISTA.Cells(1, 4).Value
If folderPath = “” Then
MsgBox “No folder path found in LISTA! Please run the listing first.”, vbExclamation
Exit Sub
End If
’ Populate dictionary with initial listing
For Each cell In wsLISTA.Range(“A1:A” & wsLISTA.Cells(wsLISTA.Rows.Count, “A”).End(xlUp).Row)
If cell.Value <> “” And Right(cell.Value, 1) <> “” Then
dictOriginal(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
’ List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet(“TempListing”)
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical folderPath, wsTemp, rowNum, 0
’ Populate dictionary with current listing
For Each cell In wsTemp.Range(“A1:A” & wsTemp.Cells(wsTemp.Rows.Count, “A”).End(xlUp).Row)
If cell.Value <> “” And Right(cell.Value, 1) <> “” Then
dictCurrent(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
Dim rowChanges As Long
rowChanges = 1
wsChanges.Cells(rowChanges, 1).Value = “Path”
wsChanges.Cells(rowChanges, 2).Value = “Change Type”
wsChanges.Cells(rowChanges, 3).Value = “Last Modified Date”
’ Find deleted files/folders
For Each key In dictOriginal.Keys
If Not dictCurrent.Exists(key) Then
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = “Deleted”
wsChanges.Cells(rowChanges, 3).Value = dictOriginal(key)
End If
Next key
’ Find added and modified files/folders
For Each key In dictCurrent.Keys
If Not dictOriginal.Exists(key) Then ’ New file/folder
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = “Added”
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
ElseIf dictCurrent(key) <> dictOriginal(key) Then ’ Modified
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = “Modified”
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
End If
Next key
’ Update LISTA sheet with current data
wsLISTA.Cells.Clear
wsLISTA.Cells(1, 4).Value = folderPath
wsTemp.UsedRange.Copy wsLISTA.Cells(1, 1)
’ Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Usage
To list the contents of the folder:
Run the ListFolderContentToSheet subroutine.
This will open a folder picker dialog where you can select the folder whose contents you want to list.
The contents will be listed hierarchically in the LISTA sheet with folders and subfolders’ names in bold.
To monitor for changes and update the LISTA sheet:
Run the MonitorFolderAndUpdateChanges subroutine.
This will compare the current state of the folder with the state listed in the LISTA sheet.
Any changes (added, deleted, or modified files) will be listed in the MODIFICARI sheet.
The LISTA sheet will also be updated to reflect the current state of the folder.
FOR THIS CODE I WANT WHEN I MAKE REFRESH OR UPDATE , DATA IN SHEET LISTA FROM COLUMS C TO REMAIN UNCHAIGED .
To ensure that data from column C remains unchanged when the LISTA sheet is updated, we can copy the existing data from column C to a temporary location before clearing and updating the sheet, and then restore it afterward. Here’s the adjustment to the code:
Adjusted MonitorFolderAndUpdateChanges Subroutine
vb
Sub MonitorFolderAndUpdateChanges()
Dim wsLISTA As Worksheet
Dim wsChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim tempArray() As Variant
Dim i As Long
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
wsChanges.Cells.Clear
' Get folder path from sheet LISTA cell D1
folderPath = wsLISTA.Cells(1, 4).Value
If folderPath = "" Then
MsgBox "No folder path found in LISTA! Please run the listing first.", vbExclamation
Exit Sub
End If
' Store existing data from column C
tempArray = wsLISTA.Range("C1:C" & wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row).Value
' Populate dictionary with initial listing
For Each cell In wsLISTA.Range("A1:A" & wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictOriginal(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical folderPath, wsTemp, rowNum, 0
' Populate dictionary with current listing
For Each cell In wsTemp.Range("A1:A" & wsTemp.Cells(wsTemp.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictCurrent(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
Dim rowChanges As Long
rowChanges = 1
wsChanges.Cells(rowChanges, 1).Value = "Path"
wsChanges.Cells(rowChanges, 2).Value = "Change Type"
wsChanges.Cells(rowChanges, 3).Value = "Last Modified Date"
' Find deleted files/folders
For Each key In dictOriginal.Keys
If Not dictCurrent.Exists(key) Then
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Deleted"
wsChanges.Cells(rowChanges, 3).Value = dictOriginal(key)
End If
Next key
' Find added and modified files/folders
For Each key In dictCurrent.Keys
If Not dictOriginal.Exists(key) Then ' New file/folder
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Added"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
ElseIf dictCurrent(key) <> dictOriginal(key) Then ' Modified
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Modified"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
End If
Next key
' Clear and update LISTA sheet with current data
wsLISTA.Cells.Clear
wsLISTA.Cells(1, 4).Value = folderPath
wsTemp.UsedRange.Copy wsLISTA.Cells(1, 1)
' Restore existing data to column C
For i = LBound(tempArray, 1) To UBound(tempArray, 1)
wsLISTA.Cells(i, 3).Value = tempArray(i, 1)
Next i
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Summary of Adjustments
Store Existing Data:
Before clearing the LISTA sheet, the existing values from column C are stored in a temporary array (tempArray).
Restore Existing Data:
After updating the LISTA sheet, the values from the temporary array are restored to column C.
With these changes, the data in column C should remain unchanged even after the update process. Ensure that you have the complete and correct setup for the other parts of the code so everything functions as expected.
To ensure that the color formatting of the cells remains unchanged when updating or refreshing the LISTA sheet, we can extend our approach to also store and restore the cell colors. Below is the updated code to preserve both the content and the color formatting of column C.
Adjustments to Preserve Cell Colors
Store Existing Cell Colors and Data:
Before clearing the LISTA sheet, the existing values and colors from column C are stored in temporary arrays.
Restore Existing Cell Colors and Data:
After updating the LISTA sheet, the values and colors from the temporary arrays are restored to column C.
Updated Code
vb
Function CreateOrRetrieveSheet(sheetName As String) As Worksheet
On Error Resume Next ' Ignore errors while attempting to get sheet
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0 ' Turn error handling back on
If CreateOrRetrieveSheet Is Nothing Then
' Sheet does not exist, create it
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets.Add
CreateOrRetrieveSheet.Name = sheetName
Else
' Clear existing content
CreateOrRetrieveSheet.Cells.Clear
End If
End Function
Sub ListFolderContentToSheet()
Dim ws As Worksheet
Set ws = CreateOrRetrieveSheet("LISTA")
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
sItem = .SelectedItems(1)
End With
ws.Cells(1, 4).Value = sItem ' Store the folder path in cell D1
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical sItem, ws, rowNum, 0
End Sub
Sub GetFolderContentHierarchical(ByVal FolderPath As String, ByVal ws As Worksheet, ByRef rowNum As Long, ByVal level As Integer)
Dim FSO As Object
Dim ParentFolder As Object
Dim FolderItem As Object
Dim FileItem As Object
Dim currentRow As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(FolderPath)
' List the folder itself
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & ParentFolder.Path & ""
ws.Cells(rowNum, 1).Font.Bold = True
rowNum = rowNum + 1
' List the items in the folder
For Each FolderItem In ParentFolder.SubFolders
GetFolderContentHierarchical FolderItem.Path, ws, rowNum, level + 1
Next FolderItem
For Each FileItem In ParentFolder.Files
If Right(FileItem.Name, 4) <> ".tmp" Then
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & FileItem.Path
ws.Cells(rowNum, 2).Value = FileItem.DateLastModified
rowNum = rowNum + 1
End If
Next FileItem
End Sub
Sub MonitorFolderAndUpdateChanges()
Dim wsLISTA As Worksheet
Dim wsChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim tempArray() As Variant
Dim tempColorArray() As Long
Dim i As Long
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
wsChanges.Cells.Clear
' Get folder path from sheet LISTA cell D1
folderPath = wsLISTA.Cells(1, 4).Value
If folderPath = "" Then
MsgBox "No folder path found in LISTA! Please run the listing first.", vbExclamation
Exit Sub
End If
' Store existing data and colors from column C
tempArray = wsLISTA.Range("C1:C" & wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row).Value
ReDim tempColorArray(LBound(tempArray, 1) To UBound(tempArray, 1))
For i = LBound(tempArray, 1) To UBound(tempArray, 1)
tempColorArray(i) = wsLISTA.Cells(i, 3).Interior.Color
Next i
' Populate dictionary with initial listing
For Each cell In wsLISTA.Range("A1:A" & wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictOriginal(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical folderPath, wsTemp, rowNum, 0
' Populate dictionary with current listing
For Each cell In wsTemp.Range("A1:A" & wsTemp.Cells(wsTemp.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictCurrent(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
Dim rowChanges As Long
rowChanges = 1
wsChanges.Cells(rowChanges, 1).Value = "Path"
wsChanges.Cells(rowChanges, 2).Value = "Change Type"
wsChanges.Cells(rowChanges, 3).Value = "Last Modified Date"
' Find deleted files/folders
For Each key In dictOriginal.Keys
If Not dictCurrent.Exists(key) Then
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Deleted"
wsChanges.Cells(rowChanges, 3).Value = dictOriginal(key)
End If
Next key
' Find added and modified files/folders
For Each key In dictCurrent.Keys
If Not dictOriginal.Exists(key) Then ' New file/folder
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Added"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
ElseIf dictCurrent(key) <> dictOriginal(key) Then ' Modified
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Modified"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
End If
Next key
' Clear and update LISTA sheet with current data
wsLISTA.Cells.Clear
wsLISTA.Cells(1, 4).Value = folderPath
wsTemp.UsedRange.Copy wsLISTA.Cells(1, 1)
' Restore existing data and colors to column C
For i = LBound(tempArray, 1) To UBound(tempArray, 1)
wsLISTA.Cells(i, 3).Value = tempArray(i, 1)
wsLISTA.Cells(i, 3).Interior.Color = tempColorArray(i)
Next i
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Summary of Additional Adjustments
Storing Cell Colors:
Cell colors from column C are stored in the tempColorArray.
Restoring Cell Colors and Data:
After updating the LISTA sheet, values and colors from the temporary arrays are restored to column C.
This updated code will ensure that not only the data but also the cell colors in column C of the LISTA sheet remain unchanged after an update or refresh.
To use the provided VBA code, you'll need to place it in the VBA editor within the Excel workbook where you want the automation to occur. Here's a step-by-step guide on how to do that:
Step-by-Step Instructions
Open the VBA Editor:
Open your Excel workbook.
Press Alt + F11 to open the VBA editor.
Insert a New Module:
In the VBA editor, go to the Insert menu and select Module. This will create a new module in the current workbook.
Paste the Code:
Copy the entire VBA code provided below and paste it into the newly created module.
Complete VBA Code
Copy the following code and paste it into the module:
vb
Function CreateOrRetrieveSheet(sheetName As String) As Worksheet
On Error Resume Next ' Ignore errors while attempting to get sheet
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0 ' Turn error handling back on
If CreateOrRetrieveSheet Is Nothing Then
' Sheet does not exist, create it
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets.Add
CreateOrRetrieveSheet.Name = sheetName
Else
' Clear existing content
CreateOrRetrieveSheet.Cells.Clear
End If
End Function
Sub ListFolderContentToSheet()
Dim ws As Worksheet
Set ws = CreateOrRetrieveSheet("LISTA")
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
sItem = .SelectedItems(1)
End With
ws.Cells(1, 4).Value = sItem ' Store the folder path in cell D1
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical sItem, ws, rowNum, 0
End Sub
Sub GetFolderContentHierarchical(ByVal FolderPath As String, ByVal ws As Worksheet, ByRef rowNum As Long, ByVal level As Integer)
Dim FSO As Object
Dim ParentFolder As Object
Dim FolderItem As Object
Dim FileItem As Object
Dim currentRow As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(FolderPath)
' List the folder itself
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & ParentFolder.Path & ""
ws.Cells(rowNum, 1).Font.Bold = True
rowNum = rowNum + 1
' List the items in the folder
For Each FolderItem In ParentFolder.SubFolders
GetFolderContentHierarchical FolderItem.Path, ws, rowNum, level + 1
Next FolderItem
For Each FileItem In ParentFolder.Files
If Right(FileItem.Name, 4) <> ".tmp" Then
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & FileItem.Path
ws.Cells(rowNum, 2).Value = FileItem.DateLastModified
rowNum = rowNum + 1
End If
Next FileItem
End Sub
Sub MonitorFolderAndUpdateChanges()
Dim wsLISTA As Worksheet
Dim wsChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim tempArray() As Variant
Dim tempColorArray() As Long
Dim i As Long
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
wsChanges.Cells.Clear
' Get folder path from sheet LISTA cell D1
folderPath = wsLISTA.Cells(1, 4).Value
If folderPath = "" Then
MsgBox "No folder path found in LISTA! Please run the listing first.", vbExclamation
Exit Sub
End If
' Store existing data and colors from column C
tempArray = wsLISTA.Range("C1:C" & wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row).Value
ReDim tempColorArray(LBound(tempArray, 1) To UBound(tempArray, 1))
For i = LBound(tempArray, 1) To UBound(tempArray, 1)
tempColorArray(i) = wsLISTA.Cells(i, 3).Interior.Color
Next i
' Populate dictionary with initial listing
For Each cell In wsLISTA.Range("A1:A" & wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictOriginal(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical folderPath, wsTemp, rowNum, 0
' Populate dictionary with current listing
For Each cell In wsTemp.Range("A1:A" & wsTemp.Cells(wsTemp.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictCurrent(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
Dim rowChanges As Long
rowChanges = 1
wsChanges.Cells(rowChanges, 1).Value = "Path"
wsChanges.Cells(rowChanges, 2).Value = "Change Type"
wsChanges.Cells(rowChanges, 3).Value = "Last Modified Date"
' Find deleted files/folders
For Each key In dictOriginal.Keys
If Not dictCurrent.Exists(key) Then
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Deleted"
wsChanges.Cells(rowChanges, 3).Value = dictOriginal(key)
End If
Next key
' Find added and modified files/folders
For Each key In dictCurrent.Keys
If Not dictOriginal.Exists(key) Then ' New file/folder
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Added"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
ElseIf dictCurrent(key) <> dictOriginal(key) Then ' Modified
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Modified"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
End If
Next key
' Clear and update LISTA sheet with current data
wsLISTA.Cells.Clear
wsLISTA.Cells(1, 4).Value = folderPath
wsTemp.UsedRange.Copy wsLISTA.Cells(1, 1)
' Restore existing data and colors to column C
For i = LBound(tempArray, 1) To UBound(tempArray, 1)
wsLISTA.Cells(i, 3).Value = tempArray(i, 1)
wsLISTA.Cells(i, 3).Interior.Color = tempColorArray(i)
Next i
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Using the Code
To List Folder Contents
Go back to Excel and press Alt + F8 to open the "Macro" dialog.
Select ListFolderContentToSheet from the list and click "Run".
Follow the folder picker dialog to select the folder whose contents you want to list.
The contents will be listed hierarchically in the LISTA sheet with folders and subfolders’ names in bold.
To Monitor Folder and Update Changes
Press Alt + F8 to open the "Macro" dialog again.
Select MonitorFolderAndUpdateChanges from the list and click "Run".
The script will compare the current state of the folder with the state listed in the LISTA sheet.
Any changes (added, deleted, or modified files) will be listed in the MODIFICARI sheet.
The LISTA sheet will also be updated to reflect the current state of the folder, while preserving the data and cell colors in column C.
Summary
By following these steps and placing the code in a module within the VBA editor, you will have a fully functional setup to list folder contents and monitor for changes, keeping the data and cell colors in column C of the LISTA sheet unchanged during updates.