Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Delete files by vba conditionally

0

 I have to delete this file But there is a condition

Kill "C:\Users\WolfieeeStyle\Desktop\1.xls"
Kill "C:\Users\WolfieeeStyle\Desktop\2.xls"
Kill "C:\Users\WolfieeeStyle\Desktop\3.xls"


If there is kill.xlsb in this path C:\Users\WolfieeeStyle\Desktop then only kill all that files (and i need a msg for the same)and if it is not present then dont kill it, do nothing(i need the msg for the same that nothing happened in a msg box)

I hope u understood what i mean to say 

If any Doubts then Plz let me know Sir/Mam

Answer
Discuss

Answers

0
Selected Answer

This code should do the job.

Private Sub KillFiles()

    Const Path As String = "C:\Users\WolfieeeStyle\Desktop\"
    
    Dim Msg As String
    Dim n As Integer
    Dim Fn() As String
    Dim i As Integer

    If Len(Dir(Path & "kill.xlsb")) Then
        ' separate file names with || (double vertical bars)
        Fn = Split("File 1.xls||File2.xls||File 3.xls", "||")
        For i = 0 To UBound(Fn)
            On Error Resume Next
            ' skip if a file doesn't exist
            Kill Path & Fn(i)
            If Err Then
                Msg = "couldn't be"
            Else
                Msg = "was successfully"
                n = n + 1
            End If
            MsgBox "File """" & fn(i) & """ & vbCr & Msg & " deleted.", _
                   vbInformation, "Action report"
        Next i
    End If
    
    MsgBox n & " files were deleted.", vbInformation, "Action report"
End Sub

In this variation you can enter as many file names as you wish. They will all be deleted only if (a) the file Kill.xlsb is found and (b) they exist.

Discuss

Discussion

Thnx Mam for the Support but i am looking for the code in which i can mention my file name in the code and it should be deleted  if  condition met 
and it will be very helpful that if files deleted  then i will get a msg for the same and if files are not deleted  then i will get a msg for the same
avinash (rep: 10) Sep 15, '19 at 6:31 am
Unless Variatus has changed the answer after seeing this entry in the discussion then it would appear to do everything you have asked for.
I very much doubt Variatus would do that without some comment. 
I therefore deduce that you have not run this or not carefully followed the simple instructions. If I am wrong, then you need to be more specific in where the short fall is.
I can see that you can "Mention my file name in the code" and that there will be a message indicating if the file was deleted or not deleted. Additional as an unrequested bonus you will get a count of the number of files deleted.
k1w1sm (rep: 197) Sep 15, '19 at 5:02 pm
Thnx Alot Variatus Mam and k1w1sm Sir for giving ur Precious Time and Great Support to this Post
Have a Great Day Sir and Mam
Problem Solved Thnx Alot
avinash (rep: 10) Sep 16, '19 at 10:46 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login