Consulting

Results 1 to 2 of 2

Thread: Delete multiple files, based on cell value.

  1. #1

    Delete multiple files, based on cell value.

    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.


    Capture1.JPG



    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

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    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
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •