How do I check if a worksheet exists or not before accessing it.
How do I check if a worksheet exists or not before accessing it.
This function will let you know if a sheet exists...
Public Function SheetExists(SName As String, _
Optional ByVal Wb As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If Wb Is Nothing Then Set Wb = ThisWorkbook
SheetExists = CBool(Len(Wb.Sheets(SName).Name))
End Function
Use it like...
if sheetexists("Sheet1") then sheets("Sheet1").Select
--
HTH...
Jim Thomlinson
"skvabbili" wrote:
>
> How do I check if a worksheet exists or not before accessing it.
>
>
> --
> skvabbili
> ------------------------------------------------------------------------
> skvabbili's Profile: http://www.excelforum.com/member.php...o&userid=34135
> View this thread: http://www.excelforum.com/showthread...hreadid=539068
>
>
If SheetExists(sheetname) Then
'
'-----------------------------------------------------------------
Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function
--
HTH
Bob Phillips
(remove xxx from email address if mailing direct)
"skvabbili" <[email protected]> wrote in
message news:[email protected]...
>
> How do I check if a worksheet exists or not before accessing it.
>
>
> --
> skvabbili
> ------------------------------------------------------------------------
> skvabbili's Profile:
http://www.excelforum.com/member.php...o&userid=34135
> View this thread: http://www.excelforum.com/showthread...hreadid=539068
>
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks