Check if an Excel worksheet exists with the same name and then delete the worksheet

Check if an Excel worksheet exists with the same name and then delete the worksheet using VBA

METHOD 1. Check if an Excel worksheet exists with the same name and then delete the worksheet using VBA

VBA

Sub Check_if_Worksheet_exists_then_Delete_Worksheet()
'declare variables
Dim ws As Worksheet
Dim check As Boolean
For Each ws In Worksheets
If ws.Name Like "Data" Then check = True: Exit For
Next
If check = True Then

Worksheets("Data").Delete

Else

MsgBox "Worksheet Name Doesn't Exist"

End If

End Sub

OBJECTS
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
ADJUSTABLE PARAMETERS
Message box: Select the message that you want Excel to display if the check is FALSE by changing the VBA code.
Worksheet Name: Select the name of the worksheet that you want to check if it already exists in the workbook by changing the Data worksheet name in the VBA code.

METHOD 2. Check if an Excel worksheet exists with the same name and then delete the worksheet using VBA

VBA

Sub Check_if_Worksheet_exists_then_Delete_Worksheet()
'declare a variable
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets("Data")
On Error GoTo 0
If Not ws Is Nothing Then

Worksheets("Data").Delete

Else

MsgBox "Worksheet Name Doesn't Exist"

End If

End Sub

OBJECTS
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
ADJUSTABLE PARAMETERS
Message box: Select the message that you want Excel to display if the check is FALSE by changing the VBA code.
Worksheet Name: Select the name of the worksheet that you want to check if it already exists in the workbook by changing the Data worksheet name in the VBA code.

Explanation about how to check if a worksheet exists with the same name and then delete the worksheet

EXPLANATION

EXPLANATION
This tutorial explains and provides step by step instructions on how to check if a worksheet exists with the same name and then delete the worksheet using VBA.

VBA Methods: Using VBA you can check if a worksheet exists with the same name and then delete the worksheet. The VBA code will go through the worksheets in the nominated workbook until it finds a worksheet with the exact name and then it will delete the worksheet. Alternatively, if it can't find a worksheet with the same name after it went through all of the worksheets, it will display a message box stating "Worksheet Name Doesn't Exist".