Delete file, based on cell name

tm0206

New Member
Joined
Feb 3, 2009
Messages
19
Hi,
I am looking for a VBA to delete a files, with the name of the file coming from a cell value.

Cell A1 = names
I would like to delete the file "names" from c:\inventory\data

I am think of using, but not sure how to include the cell into the statement?
kill c:\inventory\data\"A1".xls
 
Sorry, I forgot to put a backslash after the word data
I modified the code to have a specified path in case you will need to apply this code to a different path for some reason in the future...
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteMultipleFiles()
  Dim Path As String
  Path = "c:\inventory\data\"
  Shell Environ("comspec") & " /c DEL /Q " & """" & Join(Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp))), """ " & Path & "") & """", vbHide
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I modified the code to have a specified path in case you will need to apply this code to a different path for some reason in the future...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub DeleteMultipleFiles()
  Dim Path As String
  Path = "c:\inventory\data\"
  Shell Environ("comspec") & " /c DEL /Q " & """" & Join(Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp))), """ " & Path & "") & """", vbHide
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Thanks for the updated code, however it doesn't seem to be working. No files are being deleted?
 
Upvote 0
Without the quote marks
Sorry, I just rechecked my code and found some misplaced quotemarks/spaces. Give this (tested) version a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteMultipleFiles()
  Dim Path As String
  Path = "c:\inventory\data\"
  Shell Environ("comspec") & " /c DEL /Q " & """" & Path & Join(Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp))), """ """ & Path & "") & """", vbHide
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Sorry, I just rechecked my code and found some misplaced quotemarks/spaces. Give this (tested) version a try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub DeleteMultipleFiles()
  Dim Path As String
  Path = "c:\inventory\data\"
  Shell Environ("comspec") & " /c DEL /Q " & """" & Path & Join(Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp))), """ """ & Path & "") & """", vbHide
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Great, this works perfectly for local files. Any idea why it wouldn't work for a network path?
 
Upvote 0
Great, this works perfectly for local files. Any idea why it wouldn't work for a network path?

Sorry, no... I have been retired and, hence, not had access to a network system, for some 16 years now. I would think if the path is set up correctly, it should work, but I have no way to test that. Does it maybe work if you map your network drive to a local hard disk letter and then use it for the path?
 
Last edited:
Upvote 0
Sorry, no... I have been retired and, hence, not had access to a network system, for some 16 years now. I would think if the path is set up correctly, it should work, but I have no way to test that. Does it maybe work if you map your network drive to a local hard disk letter and then use it for the path?

I appreciate your help. I'll give the mapped drive option a go, but at least it is working locally. Thanks again
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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