Pathway to Range

sanantonio

Board Regular
Joined
Oct 26, 2021
Messages
124
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub Delete_Files()

Kill "K:\Excel\Test.xlsx"

End Sub

Hi All.

I believe I have the kill code right to delete files but rather than deleting a specific filepath I want it to delete a filepath specified in a cell? I've tried:

Kill "Range (A2)" but it doesnt work?

Any help would be appreciated as always!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:
VBA Code:
Sub Delete_Files()
    
    Dim fl As String
    fl = Range("A2").Value
    Kill fl

End Sub
Just make sure that A2 contains the complete folder path and file name (i.e. "K:\Excel\Test.xlsx").
 
Upvote 0
Sorry, I'm getting a debug error highlighting the "Kill fl". Also how do you get it to delete multiple files based on multiple cell references? Can I just repeat the code?
1642617577144.png
 
Upvote 0
It works for me.

What is the exact error message you are getting?
What exactly is in cell A2 (please post the value in this cell)?
 
Upvote 0
It works for me.

What is the exact error message you are getting?
What exactly is in cell A2 (please post the value in this cell)?
Apologies must've been something I did, I tried it in a new workbook and it worked fine. Sorry!

Any insight into making it work for multiple? A2, A3 etc. ?
 
Upvote 0
Here is code that will loop through row 2 to whatever the last populated row in column A is:
VBA Code:
Sub Delete_Files()
    
    Dim lr As Long
    Dim r As Long
    Dim fl As String
    
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows from row 2 to the last row
    For r = 2 To lr
        fl = Range("A" & r).Value
        Kill fl
    Next r

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

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