PDA

Click to See Complete Forum and Search --> : [RESOLVED] Is the sheet exist???


billhuard
Apr 20th, 2006, 01:41 PM
Hi Guys,

I'm looking for a macro or built-in fonction that can tell me, by the sheet name, if a sheet already exist in a workbook.

Excel 2003, VBA macro.

Thanks

DKenny
Apr 20th, 2006, 01:45 PM
Function SheetExists(ByVal BookName As String, ByVal SheetName As String) As Boolean
Dim wksSheet As Worksheet
Dim bTemp As Boolean

For Each wksSheet In Workbooks(BookName).Worksheets
If wksSheet.Name = SheetName Then
bTemp = True
Exit For
End If
Next wksSheet

SheetExists = bTemp

End Function

billhuard
Apr 20th, 2006, 01:51 PM
I coudn't ask for better


thanks a lot :bigyello: :bigyello: :bigyello:

DKenny
Apr 20th, 2006, 01:54 PM
I coudn't ask for better

Good, because I couldn't have done much better. ;)

DKenny
Apr 20th, 2006, 02:12 PM
Good, because I couldn't have done much better.
I lied...
You could also check to see of the workbook exists first...
Function SheetExists(ByVal BookName As String, ByVal SheetName As String) As Boolean
Dim wkbBook As Workbook
Dim wksSheet As Worksheet
Dim bTemp As Boolean

For Each wkbBook In Application.Workbooks

If wkbBook.Name = BookName Then

For Each wksSheet In wkbBook.Worksheets

If wksSheet.Name = SheetName Then
bTemp = True
Exit For
End If

Next wksSheet

End If

If bTemp Then Exit For
Next wkbBook

SheetExists = bTemp

End Function

billhuard
Apr 20th, 2006, 02:28 PM
Thanks...

but just before I check if the sheet exists I'm opening the workbook.
So I would have trapped the error before