Delete duplicate files ending in .xls

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I would like all duplicate files on Col A on sheet3 ending in .xls to be deleted


march sales.xls
march sales.xlsm
sept sales.xls

I only want march sales.xls to be deleted



Your assistance in this regard is most appreciated
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this on a copy of the folder containing the .xls and .xlsm files (the folderPath variable). File names start in A2.

Code:
Public Sub Delete_XLS_with_XLSM_Duplicate()

    Dim fileCells As Range, file As Range
    Dim folderPath As String
    Dim xlsmRow As Variant
    
    folderPath = "C:\Path\To\Folder\"   'CHANGE THIS
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    
    With Worksheets("Sheet3")
        Set fileCells = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
        For Each file In fileCells
            If Right(file.Value, 4) = ".xls" Then
                xlsmRow = Application.Match(Replace(file.Value, ".xls", ".xlsm"), fileCells, 0)
                If Not IsError(xlsmRow) Then
                    If Dir(folderPath & .Cells(xlsmRow, "A").Value) <> vbNullString Then
                        Kill folderPath & .Cells(xlsmRow, "A").Value
                        MsgBox "Deleted " & folderPath & .Cells(xlsmRow, "A").Value
                    End If
                End If
            End If
        Next
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,414
Messages
6,119,373
Members
448,888
Latest member
Arle8907

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