2

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

Code stuck here

run-time error 9

run-time error 214

Someone help me solve this issue, please.

Hasan
  • 21
  • 2
  • Start by removing `On Error Resume Next` - now what error do you get? Is it the same error? – braX Jan 18 '23 at 22:08
  • Thanks,braX. I used to comment on that line of code. However, I mentioned that and took a new snapshot as well. – Hasan Jan 18 '23 at 22:23
  • Does this answer your question? [Test or check if sheet exists](https://stackoverflow.com/questions/6688131/test-or-check-if-sheet-exists) - There are several good answers there, so don't try just the one accepted answer. – braX Jan 18 '23 at 22:24
  • You forgot `On Error Goto 0`: `On Error Resume Next: Set WS = ThisWorkbook.Sheets(SheetName): On Error Goto 0`. The colons (`:`) are denoting a new line. – VBasic2008 Jan 18 '23 at 22:30

2 Answers2

2

Added an explicit Workbook reference, and cancelling the OERN as suggested by VBasic2008

Sub Check_Sheet_Exists()
    Dim WS As Worksheet, wb As Workbook
    Dim SheetName As String  'Use Const if the name is fixed...

    SheetName = "ABCD"

    Set wb = ThisWorkbook
    On Error Resume Next 'ignore errors
    Set WS = wb.Sheets(SheetName)
    On Error GoTo 0      'stop ignoring errors

    If WS Is Nothing Then
        Set WS = wb.Worksheets.Add(before:=wb.Sheets(wb.Sheets.Count))
        WS.Name = SheetName
        MsgBox "The sheet '" & SheetName & "' was created."
    Else
        MsgBox "The sheet '" & SheetName & "' already exists."
    End If
 
End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Thanks Tim. However, that did not solve the problem. – Hasan Jan 18 '23 at 22:46
  • 1
    With the code I posted, what's the exact error and on which line? Do you have your VBA error settings set to "Break on all errors"? – Tim Williams Jan 18 '23 at 22:49
  • When I switched to *"Break on all errors"*, first I got twice the same error number with the description `Class not registered`. After that, it switched to the expected `Subscript out of range` on the first `Set` line. – VBasic2008 Jan 19 '23 at 00:16
  • I was not recommending that error setting (sorry if it seemed like that) - I'd use "Break in class module". This code works fine for me. – Tim Williams Jan 19 '23 at 00:19
  • I just meant to share how you are right about OP's issue and what happened when I switched to BOAE. I found it interesting that I initially got the exact same error number but a different description. I guess I have to become more explicit. – VBasic2008 Jan 19 '23 at 00:35
1

Add Worksheet With Specific Name

Sub AddWorksheet()
    On Error GoTo ClearError
    
    Const PROC_TITLE As String = "Add Worksheet"
    Const SHEET_NAME As String = "A\BCD"
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Check if sheet name is taken.
    
    Dim sh As Object
    
    On Error Resume Next
        Set sh = wb.Sheets(SHEET_NAME)
    On Error GoTo ClearError

    If Not sh Is Nothing Then
        MsgBox "The sheet """ & SHEET_NAME & """ already exists.", _
            vbExclamation, PROC_TITLE
        Exit Sub
    End If

    ' Add the worksheet.
    
    Dim ws As Worksheet
    Set ws = wb.Sheets.Add(Before:=wb.Sheets(wb.Sheets.Count)) ' before last
    'Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)) ' last
    'Set ws = wb.Sheets.Add(Before:=wb.Sheets(1)) ' first
        
    ' Rename the worksheet.
        
    Dim ErrNumber As Long, ErrDescription As String
    
    ' Atempt to rename.
    On Error Resume Next
        ws.Name = SHEET_NAME
        ErrNumber = Err.Number
        ErrDescription = Err.Description
    On Error GoTo ClearError
    
    ' Invalid Sheet Name.
    If ErrNumber <> 0 Then
        Application.DisplayAlerts = False ' to delete without confirmation
            ws.Delete
        Application.DisplayAlerts = True
        MsgBox "Run-time error '" & ErrNumber & vbLf & vbLf _
            & ErrDescription & vbLf & vbLf & vbLf & PROC_TITLE & " Info" _
            & vbLf & "The name """ & SHEET_NAME & _
            """ is invalid. Worksheet not added.", vbCritical, PROC_TITLE
        Exit Sub
    End If
        
    ' Valid Sheet Name
    MsgBox "The worksheet """ & SHEET_NAME & """ was added.", _
        vbInformation, PROC_TITLE

ProcExit:
    Exit Sub
ClearError:
    MsgBox "Run-time error '" & Err.Number & vbLf & vbLf _
        & Err.Description & vbLf & vbLf & vbLf & PROC_TITLE & " Info" _
        & vbLf & "An unexpected error occurred.", _
        vbCritical, PROC_TITLE
    Resume ProcExit
End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28