excel vba delete folder if exists code example

Example 1: excel vba delete worksheet if exists

For Each ws In Worksheets
    If ws.Name = "asdf" Then
        Application.DisplayAlerts = False
        Sheets("asdf").Delete
        Application.DisplayAlerts = True
    End If
Next

Sheets.Add(After:=Sheets(Sheets.count)).Name = "asdf"

Example 2: excel vba delete folder

' Needs to add "Microsoft Scripting Runtime" reference to your file
Sub DeleteFolder(pFolder As String, pDeleteReadOnlyToo As Boolean)
    Dim FSO As New FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject")
    FSO.DeleteFolder pFolder, pDeleteReadOnlyToo
End Sub

Tags:

Vb Example