Error 40036 Trying To Reference Excel WorkSheet

P

PeteCresswell

At the end of a proceess which creates an Excel .XLS, I want to see
whether or not a worksheet of a certain name has been created.

The function I'm using is below, and below that is what I see in the
Immediate window when I try to check the name of the second of two
worksheets.

The (to me, at least) obvious method of checking was to just try to
reference the worksheet in question and trap for an error - but I ran
into problems with that approach. Can't recall what the problems
were bc it was over a year ago.... but as a result, I went over to
iterating through the collection of worksheets and checking the name
of each.

This approach held up until yesterday when it started throwing an
"Application-defined or object-defined error".

Can anybody suggest an explaination/possible cause?

The routine:
-----------------------------------------------------------------------
Public Function Worksheet_Exist( _
ByVal theWorksheetName As String, _
ByRef theWB As Excel.Workbook _
) As Boolean
16000 debugStackPush mModuleName & ": Worksheet_Exist"
16001 On Error GoTo Worksheet_Exist_err

' PURPOSE: To determine if a worksheet of a given name
' exists within a specified workbook
' ACCEPTS - Name of the worksheet in question
' - Pointer to the workbook we want to check
' RETURNS: True if a sheet of given name exists, else False
'
' NOTES: 1) We got into trouble trying to do it the easy way:
namely
' just trying to set a WorkSheet pointer to the name
' in question and checking .Err.

16002 Dim k As Long
Dim i As Long

16010 k = theWB.Worksheets.Count

16990 If k > 0 Then
16991 For i = 1 To k
16992 If theWB.Worksheets(i).Name = theWorksheetName Then
16993 Worksheet_Exist = True
16994 End If
16995 Next i
16999 End If

Worksheet_Exist_xit:
DebugStackPop
On Error Resume Next
Exit Function

Worksheet_Exist_err:
BugAlert True, ""
Resume Worksheet_Exist_xit
End Function
---------------------------------------------------------------------


The Immediate Window, with code paused on 16991:
------------------------------------------
?theWB.Worksheets.Count
2

?theWB.Worksheets(1).Name
TrancheData

?theWB.Worksheets(2).Name
(throws "Application-defined or object-defined error '40036')
-------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top