Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Checking existance of a Sheet

Hi ,

I have small problem in Excel macro.

I am trying to do two processes based on existance of sheet.
If sheet exist i will perform process 1 and if sheet does not exist then process2.

i am using following code

Sheets("activity").select

but the sheet "activity" does not exist. error i got is "subscript out of range".
I want trap(skip) this error and proceed further. I need a fucntion which can give output whether sheet exist or not.

can somebody help me ?

Thanks in advance.

Rajesh
[568 byte] By [pvrajesh31] at [2007-11-11 8:03:39]
# 1 Re: Checking existance of a Sheet
Try this:

Private Sub CommandButton1_Click()
MsgBox SheetExist("activity")
End Sub

Private Function SheetExist(strSheet As String) As Boolean
On Error GoTo Errorhandling
Sheets(strSheet).Select
SheetExist = True
Exit Function

Errorhandling:
SheetExist = False
End Function

The function sheetexist gives back true if the sheet exists.
This is one solution, there are probably other.

Benjamin
Benjamin at 2007-11-11 17:26:09 >
# 2 Re: Checking existance of a Sheet
Hi Benjamin,
Thanx for your suggestion.

Its working now.

Regards,
Rajesh
pvrajesh31 at 2007-11-11 17:27:09 >