Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test if worksheet is present

Status
Not open for further replies.

davefish

Technical User
Jul 26, 2002
169
GB
I have two worksheets that are created during calculations that need to be deleted after processing by Macro. Deletion need to be carried out when a Form control button is activated, but when the sheets are not present, as they are in some instances I get errors.

The code I am using is :-


Function TheSheetExists() As Boolean
TheSheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets("Customer Quote").Name) > 0 Then
TheSheetExists = True
Exit Function
End If
NoSuchSheet:
End Function

Any Ideas would be welcome
 


hi Dave,

A function should be multifunctional, not saddled with a specific value in most cases.

In this case, supply a string value to test against all sheets in the workbook...
Code:
Function TheSheetExists(sName As String) As Boolean
    Dim ws As Worksheet
    
    TheSheetExists = True
    
    For Each ws In Worksheets
        If sName = ws.Name Then Exit Function
    Next
    
    TheSheetExists = False
End Function

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,

I tried the code but must have missed something. If I have the function in a public sub , then without refrencing the specific worksheet, how do I detect it missing?
 

You must supply a name for the function to test, like supply "Customer Quote" to the function.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top