Delete text file using VBA

Sub: Delete text file in computer using VBA

This sub deletes a file from the drive (located on a local drive or network). You can call this sub in any other sub routine by

a)       entering the complete file address (excluding the file name) in the first argument “FileAddress” field of sub. Example: “Thisworkbook.path & /MyFolder” and,

b)       entering the file name (including extension) in the 2nd argument “FileNm” field of sub. Example: “Myfile.txt”


Public Sub DelMyFile(FileAddress As Variant, FileNm As Variant)

Application.ScreenUpdating = False

‘ Code Written & Published by Excelmodelers ( 19 Dec 12

Dim WbF As Workbook

Dim objFSOF As FileSystemObject

Dim objFolderF As Folder

Dim objFileF As File


Set objFSOF = CreateObject(“Scripting.FileSystemObject”)

Set objFolderF = objFSOF.GetFolder(FileAddress)


If Not objFolderF.Files.Count = 0 Then

For Each objFileF In objFolderF.Files

If objFileF.Name = FileNm Then


End If

Next objFileF


End If

End Sub

Comments are closed.