PC Review


Reply
Thread Tools Rate Thread

Detect existence of a sheet

 
 
=?Utf-8?B?Q2xlYmVyIEluYWNpbw==?=
Guest
Posts: n/a
 
      19th Nov 2007
Hi,

I'm having problems when trying to acess a sheet in a workbook if this sheet
does'nt exist. I tried some "On Error" tricks....but it dodn't worked.
Here is the piece of code where I try to activate the sheet:
....
genesis_wb.Activate
genesis_wb.Sheets(nome_al).Activate 'nome_al is a string
....

Is there a way to check if the sheet(nome_al) exist before activating it?
(without on error techniques please).

Thanks in advance!

Cleber


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2xlYmVyIEluYWNpbw==?=
Guest
Posts: n/a
 
      19th Nov 2007
I'm didnt search in past topics for a answer before posting...sorry. i found
this function:

Function WorksheetExists(SheetName As Variant, _
Optional WhichBook As Workbook) As Boolean
'from Chip Pearson
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = CBool(Len(WB.Worksheets(SheetName).Name) > 0)
End Function


i will test it.

"Cleber Inacio" escreveu:

> Hi,
>
> I'm having problems when trying to acess a sheet in a workbook if this sheet
> does'nt exist. I tried some "On Error" tricks....but it dodn't worked.
> Here is the piece of code where I try to activate the sheet:
> ...
> genesis_wb.Activate
> genesis_wb.Sheets(nome_al).Activate 'nome_al is a string
> ...
>
> Is there a way to check if the sheet(nome_al) exist before activating it?
> (without on error techniques please).
>
> Thanks in advance!
>
> Cleber
>
>

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Nov 2007
Here is a function which you would use like this...

sub test()
if SheetExists(nome_al, genesis_wb) then
genesis_wb.Activate
genesis_wb.Sheets(nome_al).Activate 'nome_al is a string
end if
end sub

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
--
HTH...

Jim Thomlinson


"Cleber Inacio" wrote:

> Hi,
>
> I'm having problems when trying to acess a sheet in a workbook if this sheet
> does'nt exist. I tried some "On Error" tricks....but it dodn't worked.
> Here is the piece of code where I try to activate the sheet:
> ...
> genesis_wb.Activate
> genesis_wb.Sheets(nome_al).Activate 'nome_al is a string
> ...
>
> Is there a way to check if the sheet(nome_al) exist before activating it?
> (without on error techniques please).
>
> Thanks in advance!
>
> Cleber
>
>

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      19th Nov 2007
> I'm having problems when trying to acess a sheet in a workbook if this
> sheet
> does'nt exist. I tried some "On Error" tricks....but it dodn't worked.
> Here is the piece of code where I try to activate the sheet:
> ...
> genesis_wb.Activate
> genesis_wb.Sheets(nome_al).Activate 'nome_al is a string
> ...
>
> Is there a way to check if the sheet(nome_al) exist before activating it?
> (without on error techniques please).


Since you asked for a solution that doesn't use On Error techniques, here is
a function that should do that for you (the header is modeled after the
function from Chip Pearson that Cleber Inacio posted)...

Public Function SheetExists(SheetName As String, _
Optional ByVal WB As Workbook) As Boolean
Dim WS As Worksheet
If WB Is Nothing Then Set WB = ThisWorkbook
For Each WS In WB.Sheets
If StrComp(WS.Name, SheetName, vbTextCompare) = 0 Then
SheetExists = True
Exit For
End If
Next
End Function

However, I just want to point out that there is nothing wrong with using On
Error techniques to help in coding. Perhaps you are put off by the word
"error"; but you shouldn't be, there is nothing inherently "bad" about using
On Error techniques to filter out undesirable results... it's just another
way to get information for your code to work with.

Rick

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Nov 2007
I agree whole heartedly. That being said I understand Clerb's reluctance. It
is almost always better to avoid the error than it is to deal with an error.
It all depends on the nature of the error. Before you start using an error
handler you need to fully understand the error that is being generated and
determine if you could reasonably avoid it.
--
HTH...

Jim Thomlinson


"Rick Rothstein (MVP - VB)" wrote:

> > I'm having problems when trying to acess a sheet in a workbook if this
> > sheet
> > does'nt exist. I tried some "On Error" tricks....but it dodn't worked.
> > Here is the piece of code where I try to activate the sheet:
> > ...
> > genesis_wb.Activate
> > genesis_wb.Sheets(nome_al).Activate 'nome_al is a string
> > ...
> >
> > Is there a way to check if the sheet(nome_al) exist before activating it?
> > (without on error techniques please).

>
> Since you asked for a solution that doesn't use On Error techniques, here is
> a function that should do that for you (the header is modeled after the
> function from Chip Pearson that Cleber Inacio posted)...
>
> Public Function SheetExists(SheetName As String, _
> Optional ByVal WB As Workbook) As Boolean
> Dim WS As Worksheet
> If WB Is Nothing Then Set WB = ThisWorkbook
> For Each WS In WB.Sheets
> If StrComp(WS.Name, SheetName, vbTextCompare) = 0 Then
> SheetExists = True
> Exit For
> End If
> Next
> End Function
>
> However, I just want to point out that there is nothing wrong with using On
> Error techniques to help in coding. Perhaps you are put off by the word
> "error"; but you shouldn't be, there is nothing inherently "bad" about using
> On Error techniques to filter out undesirable results... it's just another
> way to get information for your code to work with.
>
> Rick
>
>

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Nov 2007
Sorry Cleber. I spelled your name incorrectly in my last post... Purely
unintentional.
--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

> I agree whole heartedly. That being said I understand Clerb's reluctance. It
> is almost always better to avoid the error than it is to deal with an error.
> It all depends on the nature of the error. Before you start using an error
> handler you need to fully understand the error that is being generated and
> determine if you could reasonably avoid it.
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Rick Rothstein (MVP - VB)" wrote:
>
> > > I'm having problems when trying to acess a sheet in a workbook if this
> > > sheet
> > > does'nt exist. I tried some "On Error" tricks....but it dodn't worked.
> > > Here is the piece of code where I try to activate the sheet:
> > > ...
> > > genesis_wb.Activate
> > > genesis_wb.Sheets(nome_al).Activate 'nome_al is a string
> > > ...
> > >
> > > Is there a way to check if the sheet(nome_al) exist before activating it?
> > > (without on error techniques please).

> >
> > Since you asked for a solution that doesn't use On Error techniques, here is
> > a function that should do that for you (the header is modeled after the
> > function from Chip Pearson that Cleber Inacio posted)...
> >
> > Public Function SheetExists(SheetName As String, _
> > Optional ByVal WB As Workbook) As Boolean
> > Dim WS As Worksheet
> > If WB Is Nothing Then Set WB = ThisWorkbook
> > For Each WS In WB.Sheets
> > If StrComp(WS.Name, SheetName, vbTextCompare) = 0 Then
> > SheetExists = True
> > Exit For
> > End If
> > Next
> > End Function
> >
> > However, I just want to point out that there is nothing wrong with using On
> > Error techniques to help in coding. Perhaps you are put off by the word
> > "error"; but you shouldn't be, there is nothing inherently "bad" about using
> > On Error techniques to filter out undesirable results... it's just another
> > way to get information for your code to work with.
> >
> > Rick
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect protected sheet IanC Microsoft Excel Programming 4 29th Sep 2009 07:13 PM
How to detect if sheet is hidden? hstijnen Microsoft Excel Worksheet Functions 1 24th Mar 2005 02:40 PM
Detect table existence =?Utf-8?B?Z2c=?= Microsoft Access Form Coding 1 5th Dec 2004 07:31 PM
Checking for existence of value in another sheet utkarshm@hotmail.com Microsoft Excel Programming 2 20th Oct 2004 02:51 PM
How do I detect if a sheet is selected? LowellSpecht Microsoft Excel Misc 3 25th Dec 2003 10:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:42 AM.