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
Dim tempArray() As Variant
Dim tempColorArray() As Long
Dim i As Long
code
End Sub
from this code I want the information to be organized vertically and hierarchically, each folder and subloader separately, each with its own files, also for column c in the MODIFICATIONS sheet, I keep the formatting, content and colors after each refresh, and all the changes that appear in the MODIFICATIONS sheet copies in the ALL CHANGES sheet, which is kept after each update and between sessions.
Assistant
To fulfill your requirements, you need to adjust your existing code and add new functionalities to retain the format, content, and colors in the "MODIFICARI" sheet after each refresh. Additionally, the changes in the "MODIFICARI" sheet will be copied to an "ALL_CHANGES" sheet that is retained between updates and across sessions. Below is the refined and complete VBA code with these modifications:
vba
User
tempArrayChanges = wsChanges.Range("C1:C" & wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row).Value run time eroor 13
Assistant
Runtime Error 13 (Type Mismatch) typically occurs when there is an inconsistency in the types of values being assigned or processed. In your case, it appears when trying to directly load the range into an array.
Let's resolve this issue step-by-step. Here is the revised MonitorFolderAndUpdateChanges procedure with added checks and corrections:
Ensure the range is valid before loading it into an array.
Modify how we handle empty cells or ranges.
Here is the updated code:
vba
User
ok write all code again wit this modification
Assistant
Certainly! Here's the complete and revised code with all the necessary modifications, ensuring we correctly handle the folder content listing, track changes, maintain formatting, and store changes in an ALL_CHANGES sheet:
vba
User
tempArrayChanges = wsChanges.Range("C1:C" & lastRowChanges).Value run time error 13 type mismatch
Assistant
The Runtime Error 13 (Type Mismatch) typically occurs when attempting to assign values or data types that are incompatible or not expected. This could be due to having no data in the specified range. Let's add appropriate checks to handle this and ensure smooth execution.
Here is the revised code with additional checks to ensure that the ranges are correctly handled, especially when assigning range values to arrays:
Apologies for the continued issues with the Range assignment. Let's take a careful approach to ensure the data is handled correctly to avoid the Runtime Error 13. One possible cause of this error could be due to the range containing different types of data or being empty. We'll add checks to handle these cases gracefully.
Here's an enhanced approach to handle the potential issues:
Check for empty ranges before assigning them to arrays.
Ensure colors are set correctly for each cell in the range.
Let's rewrite the full code with these additional checks:
vba
User
tempArrayChanges = wsChanges.Range("C1:C" & lastRowChanges).Value2 run time error 13 type mismatch
Assistant
It seems there might still be an issue with how the Range is being assigned to the array. Let's take a robust approach to ensure we're only working with valid data. We’ll incorporate additional error handling and more precise data type management to avoid any type mismatches. Furthermore, we can explicitly declare the array type to avoid any implicit type conversion issues.
Here is the revised code with detailed error handling and ensuring the array only deals with appropriate data types:
vba
User
aryChanges = wsChanges.Range("C1:C" & lastRowChanges).Value RUN TIME ERROR 13
Assistant
The Runtime Error 13 (Type Mismatch) can happen if the range being assigned to the array contains elements that are not compatible with the expected array type, or if the range dimensions are somehow causing an issue. Let's handle it with additional error checking and a more controlled method to capture the values.
Since direct assignment from a range to an array is causing issues, let's try iterating over the range and populating the arrays manually:
Steps to Address the Issue:
Manually iterate over the range to fill the array.
Ensure that only valid data is added to the array.
Here’s the revised code:
vba
User
OK BUT DOES NOT KEEP FORMAT , MODIFICATION AND COLOUR IN CLUMN c of sheet LISTA , BASIC DO NOT UPDATE THIS COLUMN .
Assistant
To ensure that the formatting, modifications, and color in column C of the "LISTA" sheet are preserved and not updated, we will:
Preserve the content and formatting of column C before clearing and updating the content from the temporary worksheet.
Restore the content and formatting to column C after updating the other columns.
Let's enhance the previous example to include these steps:
Steps to Address:
Store the content and formatting of column C before clearing the "LISTA" sheet.
Restore the content and formatting of column C after updating the "LISTA" sheet.
Here is the modified code:
vba
User
OK NOW USE ZEBRA DESIGN
Assistant
Certainly! Applying a zebra design (alternating row colors) can make your data easier to read. Let's modify the MonitorFolderAndUpdateChanges procedure to apply a zebra design to both the "LISTA" and "MODIFICARI" sheets.
We'll add a subroutine to handle the application of the zebra design, which we can call at the end of MonitorFolderAndUpdateChanges.
Key Adjustments:
Create a subroutine for the zebra design.
Call this subroutine at the appropriate point in MonitorFolderAndUpdateChanges.
Here's the updated code:
vba
User
Ok now i want in column A in sheet LISTA to keep cell colour format after refresh and between sesions .
Assistant
To ensure the cell color formatting in column A is preserved after each refresh and between sessions, we will:
Store the color formatting of column A before clearing and updating the content.
Restore the color formatting of column A after the update.
Here's how we can modify the existing code to include these steps:
Steps:
Preserve the color formatting of column A before clearing the LISTA sheet.
Restore the color formatting of column A after updating the other columns.
Here's the updated code with these enhancements:
vba
User
it does not keep cell background colour also same for column c .
Assistant
I apologize for the inconvenience. Let's take a closer look at the code to ensure we correctly preserve and restore the cell background colors in both columns A and C.
Key Adjustments:
Use distinct arrays for values and colors for both Columns A and C.
Ensure the colors are correctly restored after updating the sheet content.
Here is the revised code:
vba
Not yet included in a published release.
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
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
' Preserve existing content and formatting
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 rowNum As Long
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Store MODIFICARI existing data and colors from column C
Dim tempArrayChanges() As Variant
Dim tempColorArrayChanges() As Long
tempArrayChanges = wsChanges.Range("C1:C" & wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row).Value
ReDim tempColorArrayChanges(LBound(tempArrayChanges, 1) To UBound(tempArrayChanges, 1))
For i = LBound(tempArrayChanges, 1) To UBound(tempArrayChanges, 1)
tempColorArrayChanges(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
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")
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
' Copy changes to ALL_CHANGES
Dim wsAllChanges As Worksheet
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
' 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 MODIFICARI existing data and colors to column C
For i = LBound(tempArrayChanges, 1) To UBound(tempArrayChanges, 1)
wsChanges.Cells(i, 3).Value = tempArrayChanges(i, 1)
wsChanges.Cells(i, 3).Interior.Color = tempColorArrayChanges(i)
Next i
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Explanation of the Modifications:
Create/Retain Sheets:
CreateOrRetrieveSheet function is modified to leave the existing content and formatting intact.
Column C Handling in MODIFICARI:
The script now captures and restores existing data and colors in column C.
ALL_CHANGES Sheet:
Any changes identified during the refresh are now copied to an "ALL_CHANGES" sheet which retains its content across multiple runs and sessions.
Hierarchical and Vertical Listing:
The folder structure is printed vertically with each directory and file indented based on its level in the hierarchy.
This code ensures that the content is listed hierarchically, changes are tracked, and historical changes are retained across sessions in the "ALL_CHANGES" sheet. Make sure to run ListFolderContentToSheet first to initialize the folder listing and then you can run MonitorFolderAndUpdateChanges to track and update changes.
Sub MonitorFolderAndUpdateChanges()
Dim wsLISTA As Worksheet
Dim wsChanges As Worksheet
Dim wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim tempArrayChanges() As Variant
Dim tempColorArrayChanges() As Long
If lastRowChanges > 0 Then
tempArrayChanges = wsChanges.Range("C1:C" & lastRowChanges).Value
ReDim tempColorArrayChanges(1 To lastRowChanges)
For i = 1 To lastRowChanges
tempColorArrayChanges(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
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")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 in MODIFICARI
If lastRowChanges > 0 Then
For i = LBound(tempArrayChanges, 1) To UBound(tempArrayChanges, 1)
wsChanges.Cells(i, 3).Value = tempArrayChanges(i, 1)
wsChanges.Cells(i, 3).Interior.Color = tempColorArrayChanges(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Key Adjustments:
Array and Color Storage:
Add checks to ensure the range is valid and has contents before loading it into arrays. This avoids runtime error 13.
Copying Changes:
Copy only if there are changes identified (rowChanges > 1).
Existing Data and Colors Restoration:
Restores formatting and data only if there was any content initially.
Execution Order:
List Folder Content:
Start by running ListFolderContentToSheet to initialize the folder listing.
Monitor and Update Changes:
Subsequently, run MonitorFolderAndUpdateChanges to monitor and track changes which include handling of format and content as described.
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
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim tempArrayChanges() As Variant
Dim tempColorArrayChanges() As Long
If lastRowChanges > 0 Then
tempArrayChanges = wsChanges.Range("C1:C" & lastRowChanges).Value
ReDim tempColorArrayChanges(1 To lastRowChanges)
For i = 1 To lastRowChanges
tempColorArrayChanges(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
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")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 in MODIFICARI
If lastRowChanges > 0 Then
For i = LBound(tempArrayChanges, 1) To UBound(tempArrayChanges, 1)
wsChanges.Cells(i, 3).Value = tempArrayChanges(i, 1)
wsChanges.Cells(i, 3).Interior.Color = tempColorArrayChanges(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Explanation of Key Adjustments:
Sheet Creation and Retrieval:
The CreateOrRetrieveSheet function creates a sheet if it doesn't already exist but preserves existing content and formats if it does.
Listing Folder Content:
GetFolderContentHierarchical function lists the folder contents hierarchically and vertically.
Preserving Data and Colors:
Before clearing the MODIFICARI sheet, the script preserves existing data and cell colors from column C.
Dictionary for Change Detection:
Populates dictionaries to track changes between the initial and the latest folder contents.
Handling Changes:
Detects added, modified, and deleted files/folders, storing the results in the MODIFICARI sheet.
Copying to ALL_CHANGES:
New changes are appended to the ALL_CHANGES sheet to retain a history of all modifications.
Restoring Data and Colors:
After updating the changes, the script restores the data and colors to column C of the MODIFICARI sheet.
This setup effectively tracks changes in the folder hierarchy and maintains a record of such changes, including visual cues, across sessions.
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
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim tempArrayChanges() As Variant
Dim tempColorArrayChanges() As Long
If lastRowChanges > 0 Then
tempArrayChanges = wsChanges.Range("C1:C" & lastRowChanges).Value
ReDim tempColorArrayChanges(1 To lastRowChanges)
For i = 1 To lastRowChanges
tempColorArrayChanges(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
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")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 in MODIFICARI
If lastRowChanges > 0 Then
For i = 1 To lastRowChanges
wsChanges.Cells(i, 3).Value = tempArrayChanges(i, 1)
wsChanges.Cells(i, 3).Interior.Color = tempColorArrayChanges(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Key Adjustments:
Check for Content in Column C:
Added a condition to ensure that lastRowChanges is greater than 0 before attempting to store the range in tempArrayChanges.
This prevents the type mismatch error when the specified range has no data.
Used Variant Arrays:
The code now checks and safely initializes the variant arrays which store the colors and values, ensuring compatibility.
Execution Order:
List Folder Content:
Start by running ListFolderContentToSheet to initialize the folder listing.
Monitor and Update Changes:
Subsequently, run MonitorFolderAndUpdateChanges to monitor and track changes, handle formatting, and maintain the history in the ALL_CHANGES sheet.
This should avoid the runtime error and work as expected.
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
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim tempArrayChanges() As Variant
Dim tempColorArrayChanges() As Long
If lastRowChanges > 0 Then
tempArrayChanges = wsChanges.Range("C1:C" & lastRowChanges).Value2
ReDim tempColorArrayChanges(1 To lastRowChanges)
For i = 1 To lastRowChanges
tempColorArrayChanges(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
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")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 in MODIFICARI
If lastRowChanges > 0 Then
For i = 1 To lastRowChanges
wsChanges.Cells(i, 3).Value = tempArrayChanges(i, 1)
wsChanges.Cells(i, 3).Interior.Color = tempColorArrayChanges(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Key Adjustments:
Handling Data Type:
Changed .Value to .Value2 to ensure consistent handling of the data type when loading the range into an array.
Integrity Check on Range:
Added a check to ensure lastRowChanges is greater than 0 before attempting to access cells in the range.
Execution Order:
List Folder Content:
Run ListFolderContentToSheet to initialize the folder content listing.
Monitor and Update Changes:
Run MonitorFolderAndUpdateChanges to monitor and track changes, handle formatting, and maintain the changes in the ALL_CHANGES sheet.
This updated version should resolve the Runtime Error 13 by ensuring data type consistency and proper handling of ranges.
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
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim aryChanges() As Variant
Dim aryColors() As Long
If lastRowChanges > 0 Then
' Ensure the data to be captured is not empty
If wsChanges.Range("C1:C" & lastRowChanges).Cells.Count > 0 Then
aryChanges = wsChanges.Range("C1:C" & lastRowChanges).Value
ReDim aryColors(1 To lastRowChanges)
For i = 1 To lastRowChanges
aryColors(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
End If
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")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 in MODIFICARI
If lastRowChanges > 0 Then
For i = 1 To lastRowChanges
If Not IsEmpty(aryChanges(i, 1)) Then
wsChanges.Cells(i, 3).Value = aryChanges(i, 1)
End If
wsChanges.Cells(i, 3).Interior.Color = aryColors(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Key Adjustments:
Explicitly Declaring Arrays:
Arrays aryChanges() and aryColors() are explicitly declared to handle data and colors separately.
Pre-check on Cell Count:
Added a check to see if the cell count in the range is greater than 0 before attempting to assign it to an array.
Restoring Data and Colors Robustly:
Ensure each cell's value and color are restored correctly, even if they are mixed types or partially empty.
Execution Instructions:
List Folder Content:
Run ListFolderContentToSheet to initialize the folder listing.
Monitor and Update Changes:
Run MonitorFolderAndUpdateChanges to monitor and track changes, handle formatting, and consistently maintain the ALL_CHANGES sheet.
This approach ensures that ranges are handled robustly, avoiding type mismatch issues while maintaining the integrity of your data and formatting.
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
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim aryChanges() As Variant
Dim aryColors() As Long
If lastRowChanges > 0 Then
' Ensure there is data to process
If wsChanges.Range("C1:C" & lastRowChanges).Cells.Count > 0 Then
ReDim aryChanges(1 To lastRowChanges, 1 To 1) ' Properly dimension the array
ReDim aryColors(1 To lastRowChanges)
For i = 1 To lastRowChanges
aryChanges(i, 1) = wsChanges.Cells(i, 3).Value
aryColors(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
End If
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")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 in MODIFICARI
If lastRowChanges > 0 Then
For i = 1 To lastRowChanges
If Not IsEmpty(aryChanges(i, 1)) Then
wsChanges.Cells(i, 3).Value = aryChanges(i, 1)
End If
wsChanges.Cells(i, 3).Interior.Color = aryColors(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Key Adjustments:
Manual Iteration Over the Range:
Instead of directly assigning the range to an array, iterate through each cell and manually assign its value and color to the array. This avoids potential issues with mixed data types or problematic cells.
Explicit Array Dimensions:
Explicitly dimension the arrays using ReDim, ensuring they're properly set up to hold the range values and colors.
Error Handling:
Added checks to ensure operations are only performed if the range contents are valid.
Execution Instructions:
List Folder Content:
Run ListFolderContentToSheet.
Monitor and Update Changes:
Run MonitorFolderAndUpdateChanges to monitor and handle changes, maintaining formatting and updating the ALL_CHANGES sheet.
This new approach should robustly handle the assignment issues and avoid the Runtime Error 13.
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
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim lastRowLISTA As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim aryChanges() As Variant
Dim aryColors() As Long
If lastRowChanges > 0 Then
' Ensure there is data to process
If wsChanges.Range("C1:C" & lastRowChanges).Cells.Count > 0 Then
ReDim aryChanges(1 To lastRowChanges, 1 To 1) ' Properly dimension the array
ReDim aryColors(1 To lastRowChanges)
For i = 1 To lastRowChanges
aryChanges(i, 1) = wsChanges.Cells(i, 3).Value
aryColors(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
End If
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
' Preserve existing data and colors from column C in LISTA
lastRowLISTA = wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row
Dim aryLISTA() As Variant
Dim colorLISTA() As Long
If lastRowLISTA > 0 Then
If wsLISTA.Range("C1:C" & lastRowLISTA).Cells.Count > 0 Then
ReDim aryLISTA(1 To lastRowLISTA, 1 To 1) ' Properly dimension the array
ReDim colorLISTA(1 To lastRowLISTA)
For i = 1 To lastRowLISTA
aryLISTA(i, 1) = wsLISTA.Cells(i, 3).Value
colorLISTA(i) = wsLISTA.Cells(i, 3).Interior.Color
Next i
End If
End If
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 in LISTA
If lastRowLISTA > 0 Then
For i = 1 To lastRowLISTA
wsLISTA.Cells(i, 3).Value = aryLISTA(i, 1)
wsLISTA.Cells(i, 3).Interior.Color = colorLISTA(i)
Next i
End If
' Restore existing data and colors to column C in MODIFICARI
If lastRowChanges > 0 Then
For i = 1 To lastRowChanges
If Not IsEmpty(aryChanges(i, 1)) Then
wsChanges.Cells(i, 3).Value = aryChanges(i, 1)
End If
wsChanges.Cells(i, 3).Interior.Color = aryColors(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Key Adjustments:
Preservation in LISTA Sheet:
Added code to preserve the content and formatting of column C in the "LISTA" sheet before clearing and updating the content.
Added code to restore the content and formatting of column C in the "LISTA" sheet after updating the other columns.
General Approach:
Store existing values and colors of column C in temporary arrays.
Restore the stored values and colors after the update.
This should ensure that the column C content and formatting in both "LISTA" and "MODIFICARI" are preserved through each update.
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
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
' Apply Zebra Design
ApplyZebraDesign ws
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim lastRowLISTA As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim aryChanges() As Variant
Dim aryColors() As Long
If lastRowChanges > 0 Then
' Ensure there is data to process
If wsChanges.Range("C1:C" & lastRowChanges).Cells.Count > 0 Then
ReDim aryChanges(1 To lastRowChanges, 1 To 1) ' Properly dimension the array
ReDim aryColors(1 To lastRowChanges)
For i = 1 To lastRowChanges
aryChanges(i, 1) = wsChanges.Cells(i, 3).Value
aryColors(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
End If
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
' Preserve existing data and colors from column C in LISTA
lastRowLISTA = wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row
Dim aryLISTA() As Variant
Dim colorLISTA() As Long
If lastRowLISTA > 0 Then
If wsLISTA.Range("C1:C" & lastRowLISTA).Cells.Count > 0 Then
ReDim aryLISTA(1 To lastRowLISTA, 1 To 1) ' Properly dimension the array
ReDim colorLISTA(1 To lastRowLISTA)
For i = 1 To lastRowLISTA
aryLISTA(i, 1) = wsLISTA.Cells(i, 3).Value
colorLISTA(i) = wsLISTA.Cells(i, 3).Interior.Color
Next i
End If
End If
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 in LISTA
If lastRowLISTA > 0 Then
For i = 1 To lastRowLISTA
wsLISTA.Cells(i, 3).Value = aryLISTA(i, 1)
wsLISTA.Cells(i, 3).Interior.Color = colorLISTA(i)
Next i
End If
' Restore existing data and colors to column C in MODIFICARI
If lastRowChanges > 0 Then
For i = 1 To lastRowChanges
If Not IsEmpty(aryChanges(i, 1)) Then
wsChanges.Cells(i, 3).Value = aryChanges(i, 1)
End If
wsChanges.Cells(i, 3).Interior.Color = aryColors(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
' Apply Zebra Design to LISTA and MODIFICARI sheets
ApplyZebraDesign wsLISTA
ApplyZebraDesign wsChanges
End Sub
Sub ApplyZebraDesign(ws As Worksheet)
Dim lastRow As Long
Dim rng As Range
Dim i As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set rng = ws.Range("A1:C" & lastRow)
' Clear previous formatting
rng.Interior.ColorIndex = xlNone
' Apply zebra design
For i = 1 To lastRow
If i Mod 2 = 0 Then
rng.Rows(i).Interior.Color = RGB(220, 230, 241) ' Light blue
End If
Next i
End Sub
Explanation:
List Folder Content to Sheet:
ListFolderContentToSheet lists the folder content hierarchically and calls ApplyZebraDesign to apply the zebra design to the "LISTA" sheet.
Monitor Folder and Update Changes:
MonitorFolderAndUpdateChanges monitors and updates the changes, restores the content and formatting to columns C in both the "LISTA" and "MODIFICARI" sheets, and calls ApplyZebraDesign to apply the zebra design to both sheets.
Apply Zebra Design:
ApplyZebraDesign applies alternating row colors to the specified worksheet range, making it easier to read.
This combined functionality ensures that your data is listed hierarchically and vertically with zebra striping for readability, while also preserving formatting, modifications, and colors in column C of the "LISTA" and "MODIFICARI" sheets.
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
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
' Apply Zebra Design
ApplyZebraDesign ws
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim lastRowLISTA As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim aryChanges() As Variant
Dim aryColors() As Long
If lastRowChanges > 0 Then
' Ensure there is data to process
If wsChanges.Range("C1:C" & lastRowChanges).Cells.Count > 0 Then
ReDim aryChanges(1 To lastRowChanges, 1 To 1) ' Properly dimension the array
ReDim aryColors(1 To lastRowChanges)
For i = 1 To lastRowChanges
aryChanges(i, 1) = wsChanges.Cells(i, 3).Value
aryColors(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
End If
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
' Preserve existing data and colors from columns A and C in LISTA
lastRowLISTA = wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row
Dim aryLISTA() As Variant
Dim colorLISTA_C() As Long
Dim colorLISTA_A() As Long
If lastRowLISTA > 0 Then
If wsLISTA.Range("C1:C" & lastRowLISTA).Cells.Count > 0 Then
ReDim aryLISTA(1 To lastRowLISTA, 1 To 1) ' Properly dimension the array
ReDim colorLISTA_C(1 To lastRowLISTA)
ReDim colorLISTA_A(1 To lastRowLISTA)
For i = 1 To lastRowLISTA
aryLISTA(i, 1) = wsLISTA.Cells(i, 3).Value
colorLISTA_C(i) = wsLISTA.Cells(i, 3).Interior.Color
colorLISTA_A(i) = wsLISTA.Cells(i, 1).Interior.Color
Next i
End If
End If
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 columns A and C in LISTA
If lastRowLISTA > 0 Then
For i = 1 To lastRowLISTA
wsLISTA.Cells(i, 3).Value = aryLISTA(i, 1)
wsLISTA.Cells(i, 3).Interior.Color = colorLISTA_C(i)
wsLISTA.Cells(i, 1).Interior.Color = colorLISTA_A(i)
Next i
End If
' Restore existing data and colors to column C in MODIFICARI
If lastRowChanges > 0 Then
For i = 1 To lastRowChanges
If Not IsEmpty(aryChanges(i, 1)) Then
wsChanges.Cells(i, 3).Value = aryChanges(i, 1)
End If
wsChanges.Cells(i, 3).Interior.Color = aryColors(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
' Apply Zebra Design to LISTA and MODIFICARI sheets
ApplyZebraDesign wsLISTA
ApplyZebraDesign wsChanges
End Sub
Sub ApplyZebraDesign(ws As Worksheet)
Dim lastRow As Long
Dim rng As Range
Dim i As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set rng = ws.Range("A1:C" & lastRow)
' Clear previous formatting
rng.Interior.ColorIndex = xlNone
' Apply zebra design
For i = 1 To lastRow
If i Mod 2 = 0 Then
rng.Rows(i).Interior.Color = RGB(220, 230, 241) ' Light blue
End If
Next i
End Sub
Explanation:
Preserve Colors in Columns A and C:
We added arrays to store cell colors in both columns A and C before clearing the "LISTA" sheet.
Restoration:
Restoring the stored colors back to columns A and C after updating the "LISTA" sheet.
Zebra Design Application:
This remains the same, applying alternating row colors to make the worksheet easier to read.
With these changes, the code will now preserve the formatting (cell colors) in columns A and C, ensuring consistency between refreshes and across sessions.
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
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
' Apply Zebra Design
ApplyZebraDesign ws
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 indent As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(folderPath)
indent = String(level * 2, " ")
' List the folder itself
ws.Cells(rowNum, 1).Value = indent & 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 = indent & 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 wsAllChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Dim rowNum As Long
Dim lastRowChanges As Long
Dim lastRowLISTA As Long
Dim i As Long
' Initialize Worksheets
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set wsAllChanges = CreateOrRetrieveSheet("ALL_CHANGES")
' Initialize Dictionaries
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
' Preserve existing data and colors from column C in MODIFICARI
lastRowChanges = wsChanges.Cells(wsChanges.Rows.Count, "A").End(xlUp).Row
Dim aryChanges() As Variant
Dim aryColorsChanges() As Long
If lastRowChanges > 0 Then
' Ensure there is data to process
If wsChanges.Range("C1:C" & lastRowChanges).Cells.Count > 0 Then
ReDim aryChanges(1 To lastRowChanges, 1 To 1) ' Properly dimension the array
ReDim aryColorsChanges(1 To lastRowChanges)
For i = 1 To lastRowChanges
aryChanges(i, 1) = wsChanges.Cells(i, 3).Value
aryColorsChanges(i) = wsChanges.Cells(i, 3).Interior.Color
Next i
End If
End If
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
' Preserve existing data and colors from columns A and C in LISTA
lastRowLISTA = wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row
Dim aryLISTA_C() As Variant
Dim colorLISTA_C() As Long
Dim colorLISTA_A() As Long
If lastRowLISTA > 0 Then
ReDim aryLISTA_C(1 To lastRowLISTA, 1 To 1) ' Properly dimension the array
ReDim colorLISTA_C(1 To lastRowLISTA)
ReDim colorLISTA_A(1 To lastRowLISTA)
For i = 1 To lastRowLISTA
aryLISTA_C(i, 1) = wsLISTA.Cells(i, 3).Value
colorLISTA_C(i) = wsLISTA.Cells(i, 3).Interior.Color
colorLISTA_A(i) = wsLISTA.Cells(i, 1).Interior.Color
Next i
End If
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
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
' Copy changes to ALL_CHANGES
Dim lastRowAllChanges As Long
lastRowAllChanges = wsAllChanges.Cells(wsAllChanges.Rows.Count, "A").End(xlUp).Row + 1
If rowChanges > 1 Then
wsChanges.Range("A2:C" & rowChanges).Copy wsAllChanges.Cells(lastRowAllChanges, 1)
End If
' 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 columns A and C in LISTA
If lastRowLISTA > 0 Then
For i = 1 To lastRowLISTA
wsLISTA.Cells(i, 3).Value = aryLISTA_C(i, 1)
wsLISTA.Cells(i, 3).Interior.Color = colorLISTA_C(i)
wsLISTA.Cells(i, 1).Interior.Color = colorLISTA_A(i)
Next i
End If
' Restore existing data and colors to column C in MODIFICARI
If lastRowChanges > 0 Then
For i = 1 To lastRowChanges
If Not IsEmpty(aryChanges(i, 1)) Then
wsChanges.Cells(i, 3).Value = aryChanges(i, 1)
End If
wsChanges.Cells(i, 3).Interior.Color = aryColorsChanges(i)
Next i
End If
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
' Apply Zebra Design to LISTA and MODIFICARI sheets
ApplyZebraDesign wsLISTA
ApplyZebraDesign wsChanges
End Sub
Sub ApplyZebraDesign(ws As Worksheet)
Dim lastRow As Long
Dim rng As Range
Dim i As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set rng = ws.Range("A1:C" & lastRow)
' Clear previous formatting
rng.Interior.ColorIndex = xlNone
' Apply zebra design
For i = 1 To lastRow
If i Mod 2 = 0 Then
rng.Rows(i).Interior.Color = RGB(220, 230, 241) ' Light blue
End If
Next i
End Sub
Key Adjustments:
Preserve Color Formatting:
Arrays colorLISTA_A, colorLISTA_C, and aryLISTA_C are used to store the background colors and values for columns A and C of the "LISTA" sheet before clearing it.
Arrays aryChanges and aryColorsChanges are used to store the values and background colors for column C of the "MODIFICARI" sheet.
Restore Color Formatting:
After updating the content, the colors and values stored in the arrays are restored to their respective cells in columns A and C of the "LISTA" sheet and column C of the "MODIFICARI" sheet.
This approach should ensure that background colors in both columns A and C are preserved after every refresh and between sessions.