Determine If Worksheet Exists

AllisterB

Board Regular
Joined
Feb 22, 2019
Messages
120
Office Version
  1. 365
Platform
  1. Windows
The attached Fil has a macro that checks if the Sheet "Errors" exists and if so it clears the Sheet.

It then searches and lists the errors on the Errors Sheet

It then Dates Stamps the Sheet

Do I need any error handling for this part of the macro ?

The problem is that if the Sheet doesn't exist then it errors.

Any suggestions

Thank You

Allister



VBA Code:
Sub List_Errors()
  Dim rErrors As Range, r As Range
  Dim i As Long, nr As Long
  Dim sName As String
  
  If (Worksheets("Errors").Name <> "") Then
       Sheets("Sheet1").Cells.ClearContents
       Else
       Sheets.Add Before:=Sheets(1)
       ActiveSheet.Name = "Errors"
  End If
  
  
  nr = 1
  For i = 2 To Sheets.Count
    Set rErrors = Nothing
    On Error Resume Next
    Set rErrors = Sheets(i).UsedRange.SpecialCells(xlFormulas, xlErrors)
    On Error GoTo 0
    If Not rErrors Is Nothing Then
      sName = Sheets(i).Name
      For Each r In rErrors
        nr = nr + 1
        With Sheets(1)
          .Cells(nr, 1).Value = sName
          .Cells(nr, 2).Value = r.Address(0, 0)
          .Cells(nr, 3).Value = r.Text
        End With
      Next r
    End If
  Next i
  Sheets(1).Range("A1:C1").Value = Array("Sheet", "Cell", "Error")
  
  'Add Header
   
   Range("A1").Select
    ActiveCell.FormulaR1C1 = "As Of "
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=NOW()"
    Range("B1").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1:B1").Select
    Selection.Font.Bold = True
  
End Sub


Errors.xlsm
D
33
Sheet2
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi AllisterB,

Try this:

VBA Code:
Option Explicit
Sub List_Errors()
  
    Dim rErrors As Range, r As Range
    Dim i As Long, nr As Long
    Dim sName As String
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
  
    On Error Resume Next
        Set ws = ThisWorkbook.Sheets("Errors")
        If Err.Number <> 0 Then
            Sheets.Add(Before:=ThisWorkbook.Sheets(1)).Name = "Errors"
        Else
            ws.Cells.ClearContents
        End If
    On Error GoTo 0
  
'  If (Worksheets("Errors").Name <> "") Then
'       Sheets("Sheet1").Cells.ClearContents
'       Else
'       Sheets.Add Before:=Sheets(1)
'       ActiveSheet.Name = "Errors"
'  End If
  
    nr = 1
    For i = 2 To Sheets.Count
        Set rErrors = Nothing
        On Error Resume Next
            Set rErrors = Sheets(i).UsedRange.SpecialCells(xlFormulas, xlErrors)
        On Error GoTo 0
        If Not rErrors Is Nothing Then
            sName = Sheets(i).Name
            For Each r In rErrors
                nr = nr + 1
                With Sheets(1)
                    .Cells(nr, 1).Value = sName
                    .Cells(nr, 2).Value = r.Address(0, 0)
                    .Cells(nr, 3).Value = r.Text
                End With
            Next r
        End If
    Next i
    Sheets(1).Range("A1:C1").Value = Array("Sheet", "Cell", "Error")
  
  'Add Header
   
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "As Of "
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=NOW()"
    Range("B1").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1:B1").Select
    Selection.Font.Bold = True
    
    Application.ScreenUpdating = True
  
End Sub

Regards,

Robert
 
Upvote 0
What say the workbook doesn't have a sheet called "Errors" - what part of eth code handles this ?
 
Upvote 0
The first block of code i.e. this:

VBA Code:
On Error Resume Next
        Set ws = ThisWorkbook.Sheets("Errors")
        'If there's an error setting the 'ws' variable, then...
        If Err.Number <> 0 Then
            '...there's no sheet called 'Errors' in the workbook, so create it as the the first tab
            Sheets.Add(Before:=ThisWorkbook.Sheets(1)).Name = "Errors"
        'Else...
        Else
            '...it does exist so just clear its contents
            ws.Cells.ClearContents
        End If
    On Error GoTo 0

I've added comments to show you what the code is doing.

Thanks,

Robert
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top