I am in need of assistance in resolving an issue I have been experiencing when running certain codes, particularly the one listed below. While I comprehend why this error message may appear, I am unsure as to why it is occurring with this particular code. I have been receiving the 'expecting object to be local' error message and 'subscript out of range error message when no sheet with the name is declared in the, despite the code's purpose being to determine if the sheet is already present or not and create it if it does not exist.
I'll be thankful to those who would put a hand into this.
By the way, the code stops at the step of setting the WS.
Sub Check_Sheet_Exists()
Dim WS As Worksheet
Dim SheetName As String
SheetName = "ABCD"
' On Error Resume Next
Set WS = ThisWorkbook.Sheets(SheetName)
If WS Is Nothing Then
Sheets.Add before:=Sheets(Sheets.Count)
ActiveSheet.Name = SheetName
MsgBox "The sheet '" & SheetName & "' was created."
Else
MsgBox "The sheet '" & SheetName & "' already exists."
End If
End Sub
Someone help me solve this issue, please.