VBA to delete specific files from a specific path listed within a worksheet in excel?

dougmarkham

Active Member
Joined
Jul 19, 2016
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I am looking for VBA code to find file names listed in a Worksheet in range A2 downward e.g., XYZ.pdf located in the filepath listed in column B2 downwards and then to delete those files (killfile).

I have found multiple examples of VBA to kill files e.g., all files in a folder, all files of a specific type in a folder, or all files containing some element in their filename; however, I cannot find anyone who has published code for killing specific files listed in a worksheet with cognate specific path.

Does anybody know how to do this and be willing to share this code?

Kind regards,

Doug.
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Put some filenames with extensions in A2 down then run this. Be careful that you do actually want them deleted!!!

Code:
filepath = ThisWorkbook.Path 'for example, target folder for deleting files change as appropriate
lr = Range("A" & Rows.Count).End(xlUp).Row

For Each c In Range("A2:A" & lr)
    If Len(c) > 0 Then
        If Len(Dir(filepath & "\" & c.Value)) > 0 Then
            Kill filepath & "\" & c.Value
        End If
    End If
Next
 
Upvote 0
Put some filenames with extensions in A2 down then run this. Be careful that you do actually want them deleted!!!

Code:
filepath = ThisWorkbook.Path 'for example, target folder for deleting files change as appropriate
lr = Range("A" & Rows.Count).End(xlUp).Row

For Each c In Range("A2:A" & lr)
    If Len(c) > 0 Then
        If Len(Dir(filepath & "\" & c.Value)) > 0 Then
            Kill filepath & "\" & c.Value
        End If
    End If
Next

Hi Steve,

Steve, that is an awesome bit of code, thank you!
I just saved it into the folder my files resided in, ran the VBA and instantly all the files listed in Column A were deleted.
Very elegant piece of code, considering the length of some killfile VBA code I've seen.

Many thanks again,

Doug.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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