Could anyone tell me where i have gone wrong with this code. The idea is to delete the files if the set date is after the current date, otherwise perform the import of data from the 0. master cost sheet into the table.
Much appreciated
Much appreciated
Private Sub Workbook_Open()
Dim fileDirectory As String
Dim fileName As String
Dim fileToOpen As Workbook
Dim killDate As Date
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
ws.Unprotect Password:="******"
Next ws
Sheets("HOME").Select
fileDirectory = "C:\Users\" & Environ$("username") & "\Dropbox\Test\"
fileName = Dir(fileDirectory)
Do While Len(fileName) > 0
Set fileToOpen = Workbooks.Open(fileDirectory & fileName)
killDate = CDate(fileToOpen.Worksheets("HOME").Range("killDate").Value)
fileToOpen.Close False
'If killDate < IDate Then
If killDate < DateSerial(2022, 4, 15) Then
' Delete the file.
Kill fileDirectory & fileName
Else
' File should not be deleted
'
' OpenWorkbook Macro
For Each ws In ActiveWorkbook.Worksheets
ws.Unprotect Password:="******"
Next ws
Sheets("HOME").Select
'Open method requires full file path to be referenced.
Workbooks.Open "C:\Users\" & Environ$("username") & "\Dropbox\Synagogue Management\Bars\Cost Sheets\0. Master- Cost Sheet.xlsm"
'Open method has additional parameters
'Workbooks.Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad)
'Help page: Workbooks.Open method (Excel)
'Copy range to clipboard
Workbooks("0. Master- Cost Sheet.xlsm").Worksheets("STOCKLIST").Range("Table1").Copy
'PasteSpecial to paste values, formulas, formats, etc.
Application.DisplayAlerts = False
ThisWorkbook.Worksheets("STOCKLIST").Range("Table1").PasteSpecial Paste:=xlPasteAll
Workbooks("0. Master- Cost Sheet.xlsm").Worksheets("SUPPLIERS").Range("Table6").Copy
'PasteSpecial to paste values, formulas, formats, etc.
ThisWorkbook.Worksheets("SUPPLIERS").Range("Table6").PasteSpecial Paste:=xlPasteAll
Application.DisplayAlerts = False
Application.CutCopyMode = False
Workbooks("0. Master- Cost Sheet.xlsm").Close SaveChanges:=False
'Close method has additional parameters
'Workbooks.Close(SaveChanges, Filename, RouteWorkbook)
'Help page: Workbook.Close method (Excel)
Worksheets("HOME").Activate
Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name <> "HOME" Then sh.Visible = xlSheetHidden
Next
End If
fileName = Dir
Loop
Application.ScreenUpdating = True
End Sub