Kill file error

gleamng

Board Regular
Joined
Oct 8, 2016
Messages
98
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi đź‘‹ everyone, I hope you are all doing great.
Please help me out with this error code.
I tried using the kill command but it return "runtime error 70 - permission denied" , meanwhile the file i have the macro is also in the same folder and to be deleted as well at a particular date. Below is my code

Sub expiry()
Dim expiry as Date
Expiry = "26 Feb, 2023"
If Date > Expiry Then
Kill "D:\Google Drive\MSC\*.*"
End If
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I don't think you can delete a folder if the file the VBA code is contained in is in a workbook in that same folder (that you are trying to delete)!
You usually need to be logged out of any files in the folder you want to delete.

Your best bet is to have this workbook with VBA code is some other folder than the one you are trying to delete!
 
Upvote 0
Thanks for your quick response.
Change of plans. I want the code to delete another opened workbook, but it need to close it without saving i.e D:\Google Drive\MSC\DataBase.xlsm

Sub expiry()
Dim expiry as Date
Expiry = "26 Feb, 2023"
If Date > Expiry Then
Workbooks ("D:\Google Drive\MSC\DataBase.xlsm"). Close
Kill "D:\Google Drive\MSC\DataBase.xlsm"
End If
End Sub
 
Upvote 0
I've been using kill command but it's not deleting the file
 
Upvote 0
Try changing
VBA Code:
Expiry = "26 Feb, 2023"

to
VBA Code:
Expiry = DateSerial(2023,  2, 26)

Obviously it will only work when your system date is later than 26 Feb, 2023
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
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