Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Detect existence of a sheet

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Detect existence of a sheet

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Detect existence of a sheet

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Detect existence of a sheet

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Detect existence of a sheet

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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Detect existence of a sheet

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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect for empty excel sheet [email protected] Excel Programming 4 January 4th 07 08:32 AM
detect and delete existing sheet swiftcode Excel Discussion (Misc queries) 3 September 16th 05 05:20 PM
How to detect if sheet is hidden? hstijnen Excel Worksheet Functions 1 March 24th 05 02:40 PM
Checking for existence of value in another sheet [email protected] Excel Programming 2 October 20th 04 02:51 PM
Detect Password Protection on a Sheet Chris Gorha Excel Programming 2 January 10th 04 09:17 AM


All times are GMT +1. The time now is 07:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"