Check if an Excel worksheet name doesn't exist and then add a worksheet

How to check if an Excel worksheet name doesn't exist and then add a worksheet using VBA

METHOD 1. Check if an Excel worksheet name doesn't exist and then add a worksheet using VBA

VBA

Sub Check_if_Worksheet_exists_then_Add_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

MsgBox "Worksheet Name Already Exists"

Else

Worksheets.Add.Name = "Data"

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 TRUE 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.

ADDITIONAL NOTES
Note 1: Running this VBA code will bring up a message box stating if the workbook already has an existing worksheet with the same name.

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

VBA

Sub Check_if_Worksheet_exists_then_Add_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

MsgBox "Worksheet Name Already Exists"

Else

Worksheets.Add.Name = "Data"

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 TRUE 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.

ADDITIONAL NOTES
Note 1: Running this VBA code will bring up a message box stating if the workbook already has an existing worksheet with the same name.

Explanation about how to check if a worksheet name doesn't exist and then add a worksheet

EXPLANATION

EXPLANATION
This tutorial explains and provides step by step instructions on how to check if a worksheet name doesn't exist and then add a worksheet using VBA.

VBA Methods: Using VBA you can check if a worksheet name doesn't exist and then add a worksheet. The VBA code will go through each of the worksheets in the nominated workbook and if it finds a worksheet with the exact name it will not insert a new worksheet, alternatively, if a worksheet with the same name doesn't exist it will insert the new worksheet.