File delete

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the following code:

Code:
Sub DeleteFile ()
Dim TestFile As workbook
Set TestFile = ActiveWorkbook
 
' switch to another defined file
' do some stuff
 
TestFile.Close Save = False
Kill TestFile
 
End Sub

The macro stops at the Kill TestFile line with error message:
"Run-time error '424': Object required"

I can't determine what I'm doing wrong, I thought by setting TestFile as a workbook it is the object?

Thanks,
Jack
 

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.
Try

Code:
Sub DeleteFile()
Dim TestFile As Workbook, strFile As String
Set TestFile = ActiveWorkbook
strFile = TestFile.FullName
' switch to another defined file
' do some stuff
 
TestFile.Close Savechanges:=False
Kill strFile
 
End Sub
 
Upvote 0
Thanks VoG, that works great

However, I'm already saving the full name of the file into a named range in another workbook. Will the line below work, or can you amend it please so that it does?

Code:
Kill Range("NewSummaryFile")

E.g. A1 is renamed as NewSummaryFile and I'm storing the full path name of the file that needs to be deleted in A1

Thanks,
Jack
 
Upvote 0
I think that you would need to qualify the workbook and the sheet:

Code:
Kill Workbooks("name").Sheets("sheet name").Range("NewSummaryFile")
 
Upvote 0
Delete me doesn't work

Kill Range("NewSummaryFile") just worked though I am in the activeworkbook where the range is named. I did this yesterday but it didn't work and nothing's changed but oh well it works now!

Thanks,
Jack
 
Upvote 0
lol sorry Aaron - out of interest, how do you delete a post or close it once your query is solved? Always wondered about that..
 
Upvote 0
You don't, you acknowledge the problem as solved then leave the thread for other people who have the same problem to find:)
 
Upvote 0
In that case, I believe it's solved! (Well it's currently working anyway...!)
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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