Tuesday, April 12, 2016

How to delete a excel workbook from selected folder?

Delete specific workbook or excel file is a usual task in many reporting project. As a routine activity user needs to delete old report and create new one based on latest data. Performing delete activity seems to be very risky when it is a manual, specifically when folder contain several report with slightly change in names.

Since to delete old files from the specific folder, user need to automate this task in excel macro itself. Automating file deletion task not only accomplish 100% quality but also saves a lot time to delete old files if there are several files to be delete from the folder

Here is the code which can used to delete specific file in the folder:

Sub Delete_File()
' Author: Dreams24
' Written for VBA Tricks and tips blog
' https://vbatricksntips.com

Dim fName As String


' Specify your file path which needs to be deleted

    fName = "C:\Test.xlsx"

    If Len(Dir(fName)) <> 0 Then

        Kill fName
        MsgBox "File Deleted Successfully", vbInformation
    End If

End Sub

No comments: