PDA

View Full Version : Delete multiple files, based on cell value.



DimitriosNik
07-19-2018, 09:03 AM
Hi,

I'm trying to delete a file, running VBA code where:
based on a cell value, the program looks for a file with the same name and then deletes it.

Here it looks for INACTIVE files, moves 1 cell to the right and then deletes the appropriate files from a folder.
Help would be much appreciated. :)


22590



This is how far I've got:

Sub delete_INACTIVE_files()
Dim r As Range
Dim path As String
Dim file_name As String
Dim actsh As Worksheet
Set actsh = ActiveSheet
Application.ScreenUpdating = False

Set r = ActiveSheet.Cells(1, 1)
Do Until r = ""
If UCase(r.Value) Like "INACTIVE" Then
ActiveCell.Offset(0, 1).Select

'I don't know how to insert the file name in the kill function
Kill ("C:\Users\NikolouzosD\AppData\Local\Temp\vbakillfunction" & file_name)



End If
Set r = r.Offset(1, 0)
Loop
End If
End Sub

georgiboy
07-19-2018, 09:13 AM
How about:

Sub delete_INACTIVE_files()
Dim rCell As Range, fileName As String

For Each rCell In Sheet1.Range("A1:A" & Sheet1.Range("A" & Rows.Count).End(xlUp).Row).Cells
If UCase(rCell.Value) Like "INACTIVE" Then
Kill ("C:\Users\NikolouzosD\AppData\Local\Temp\vbakillfunction\" & rCell.Offset(, 1).Value)
End If
Next rCell


End Sub

Untested

Hope this helps