Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to delete files from an Excel list 1

Status
Not open for further replies.

richiwatts

Technical User
Jun 21, 2002
180
GB
I have an Excel sheet with a list of files with full path

a1 = C:\Documents and Settings\rich\My Documents\test1.doc
a2 = C:\Documents and Settings\rich\My Documents\new\test1.gif
a3 = C:\Documents and Settings\rich\My Documents\new\people.gif

...and so on. I have 600 rows like this and I need to delete all the listed files. Does anyone know of an easy way to do this

Rich
 
Rich,

Something like the following should work:
Code:
Sub DeleteListedFiles()
Dim i As Long
Dim FName As String

   With Worksheets("SheetName")
     For i = 1 To 600
       FName = .Cells(i, 1).Value
       If FileExists(FName) Then
         Kill FName
       End If
     Next i
   End With
   
End Sub

Function FileExists(ByVal FName As String) As Boolean
   FileExists = (Dir(FName) <> "")
End Function
Change "SheetName" to the name of the worksheet containing the listed files.
Change For..Next loop enpoint to actual number.

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top