Delete file from Dropbox after expiry date VBA

BODYCOTE

New Member
Joined
May 18, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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

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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top