Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default How to check for the existance of a Sheet (or not)

Hi to all

I have writen a routine to analyse an e-telephone account
The idea is that it should create a new sheet (if it doesn't exist) for each
different telephone number it comes accross.
I use an error-check for the existance of the sheet. If an error occurs
(i.e. fails the 'Select' sheet line it then goes to the lable 'NoSuchSheet'
where it copies a template and then re-names it.

All works well if only one new telephone sheet needs to be created. ie On
Error works only once but 'bombs' the second time.

I am looking for an alternative way of checking for the existance of a sheet
without incurrring an error.

This snipit works if only one sheet is not in existance
For Each TelNo In rngTelNos
On Error GoTo 0
If TelNo < "" Then
SheetTelNoName = "Acc_" & TelNo
On Error GoTo NoSuchSheet
Sheets(SheetTelNoName).Select
On Error GoTo 0
GoTo FoundSheet
NoSuchSheet:
On Error GoTo 0
Sheets("Acc Template").Visible = True
Sheets("Acc Template").Select
Sheets("Acc Template").Copy After:=Sheets(2)
Sheets("Acc Template").Visible = False
Sheets("Acc Template (2)").Select
Sheets("Acc Template (2)").Name = SheetTelNoName
Sheets("All Accounts").Select
FoundSheet:
End If
On Error GoTo 0
Next TelNo




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How to check for the existance of a Sheet (or not)

Once an error has been raised, VBA is executing in "error mode",
and On Error statements will have no effect. You need to restore
VBA to "normal mode" with a Resume statement.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Pete" wrote in message
...
Hi to all

I have writen a routine to analyse an e-telephone account
The idea is that it should create a new sheet (if it doesn't
exist) for each
different telephone number it comes accross.
I use an error-check for the existance of the sheet. If an
error occurs
(i.e. fails the 'Select' sheet line it then goes to the lable
'NoSuchSheet'
where it copies a template and then re-names it.

All works well if only one new telephone sheet needs to be
created. ie On
Error works only once but 'bombs' the second time.

I am looking for an alternative way of checking for the
existance of a sheet
without incurrring an error.

This snipit works if only one sheet is not in existance
For Each TelNo In rngTelNos
On Error GoTo 0
If TelNo < "" Then
SheetTelNoName = "Acc_" & TelNo
On Error GoTo NoSuchSheet
Sheets(SheetTelNoName).Select
On Error GoTo 0
GoTo FoundSheet
NoSuchSheet:
On Error GoTo 0
Sheets("Acc Template").Visible = True
Sheets("Acc Template").Select
Sheets("Acc Template").Copy After:=Sheets(2)
Sheets("Acc Template").Visible = False
Sheets("Acc Template (2)").Select
Sheets("Acc Template (2)").Name = SheetTelNoName
Sheets("All Accounts").Select
FoundSheet:
End If
On Error GoTo 0
Next TelNo






  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default How to check for the existance of a Sheet (or not)

Hi Pete

You can use a function in your module

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

And use this in your macro

If SheetExists("test") = False Then
'.....
End If



--
Regards Ron de Bruin
http://www.rondebruin.nl



"Pete" wrote in message ...
Hi to all

I have writen a routine to analyse an e-telephone account
The idea is that it should create a new sheet (if it doesn't exist) for each
different telephone number it comes accross.
I use an error-check for the existance of the sheet. If an error occurs
(i.e. fails the 'Select' sheet line it then goes to the lable 'NoSuchSheet'
where it copies a template and then re-names it.

All works well if only one new telephone sheet needs to be created. ie On
Error works only once but 'bombs' the second time.

I am looking for an alternative way of checking for the existance of a sheet
without incurrring an error.

This snipit works if only one sheet is not in existance
For Each TelNo In rngTelNos
On Error GoTo 0
If TelNo < "" Then
SheetTelNoName = "Acc_" & TelNo
On Error GoTo NoSuchSheet
Sheets(SheetTelNoName).Select
On Error GoTo 0
GoTo FoundSheet
NoSuchSheet:
On Error GoTo 0
Sheets("Acc Template").Visible = True
Sheets("Acc Template").Select
Sheets("Acc Template").Copy After:=Sheets(2)
Sheets("Acc Template").Visible = False
Sheets("Acc Template (2)").Select
Sheets("Acc Template (2)").Name = SheetTelNoName
Sheets("All Accounts").Select
FoundSheet:
End If
On Error GoTo 0
Next TelNo






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
Check Activesheet for chart sheet or work sheet NSK Charts and Charting in Excel 1 July 17th 07 09:00 PM
Enable check box in protected sheet + group check boxes Dexxterr Excel Discussion (Misc queries) 4 August 2nd 06 12:00 PM
how to use sumif function to check date in 1 sheet is < 2 sheet Bharat Saboo Excel Worksheet Functions 3 December 30th 05 07:10 AM
error handling - check chart existance annette2002[_2_] Excel Programming 1 June 9th 04 06:03 AM
Existance Check Fails ChuckM[_2_] Excel Programming 7 January 31st 04 03:02 AM


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

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

About Us

"It's about Microsoft Excel"