i currently have this code that iterates through my folder for file names and then add it into the dictionary, if file name does not exists, then "do something" - (which is to add filename into excel sheet) else "do nothing" however when filename exists, and it reaches the "do nothing" part it will wipe out the filenames in the excel sheet and leave the sheet blank.
I know that it is due to these line of codes Dim wks As Worksheet
Set wks = CreateOutputSheet(ActiveWorkbook)
, the function code is provided below, can anyone advice me on how i can change the function code or main code so that when dictionary exists, it will literally do nothing to the data in the sheet. But i must keep the sheet name as "Data"
Thank you so much.
Public Dict As Object
Sub Test1()
Dim oFSO As Object, oFolder As Object, oFile As Object
If Dict Is Nothing Then
Set Dict = CreateObject("Scripting.Dictionary")
Dict.Add Key:="filename", Item:=oFile
End If
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\Users\Dekstop\")
Dim wks As Worksheet
Set wks = CreateOutputSheet(ActiveWorkbook)
For Each oFile In oFolder.Files
If Not Dict.Exists(oFSO.GetBaseName(oFile)) Then
' Do something
Else
' Do nothing
End If
Next oFile
End Sub
Private Function CreateOutputSheet(ByVal book As Workbook) As Worksheet
Dim wks As Worksheet
Application.DisplayAlerts = False
For Each wks In ActiveWorkbook.Worksheets
If wks.Name = "Data" Then
wks.Delete
End If
Next wks
Application.DisplayAlerts = True
Set wks = book.Worksheets.Add(After:=book.Worksheets(book.Worksheets.count))
wks.Name = "Data"
AddColumnHeaders wks
Set CreateOutputSheet = wks
End Function