Have you ever needed to delete a folder and its contents using VBA? This tip shows you how this can be done. The first thing you will need to do is to install the references to the Windows Script Host Model. To do so, open VBE (Alt+F11); then click on Tools --> References. Look for Windows Script Host Model select it and install it.
Then you can use the following code to get the job done:
Sub TestFunction()
MsgBox DeleteFolder("C:\TestFolder")
End Sub
Function DeleteFolder(ByVal strFolderPath As String) As Boolean
Dim fsoObj As New FileSystemObject
Dim delFolder As Folder
On Error Resume Next
Set delFolder = fsoObj.GetFolder(strFolderPath)
If delFolder Is Nothing Then GoTo ExitFunction
delFolder.Delete True
DeleteFolder = True
ExitFunction:
Set fsoObj = Nothing
End Function