Macro to delete specific files in C:\test

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have setup up some files names with wilcards in Col O on sheet data

eg
eWhs*.*
InterestXls.Xls

The InterestXls.Xls is being deleted but nome of the eWhs*.* files eg eWhs_sales_Inquiry.xlsx, eWhs_sale_Inquiry (1).xlsx, eWhs_Statement_Inquiry.xlsx, eWhs_Statement_Inquiry (2).xlsx etc

Code:
 Sub DeleteFiles()
Dim Filepath As String

Filepath = "C:\test"
Sheets("data import").Select

LR = Cells(Rows.Count, "O").End(xlUp).Row

For Each c In Range("O2:O" & LR)
    If Len(c) > 0 Then
        If Len(Dir(Filepath & "\" & c.Value)) > 0 Then
            Kill Filepath & "\" & c.Value
        End If
    End If
Next
 End Sub


It would be appreciated if someone could assist me
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi howard
Your code looks good but you just need to add 1 to your LR & make sure your first file name is in O2 as you are checking the range from row 2

Code:
 Sub DeleteFiles()
Dim Filepath As String

Filepath = "C:\test"
Sheets("data import").Select

LR = Cells(Rows.Count, "O").End(xlUp).Row + 1

For Each c In Range("O2:O" & LR)
    If Len(c) > 0 Then
        If Len(Dir(Filepath & "\" & c.Value)) > 0 Then
            Kill Filepath & "\" & c.Value
        End If
    End If
Next
 End Sub
[code]
 
Upvote 0
Thanks for the reply

I had the first file in O1 and changed
Code:
 For Each c In Range("O2:O" & LR)  

to


For Each c In Range("O1:O" & LR)

I only picked this up after you replied and I checked my code again
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

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