Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Does a sheet exist

Given a sheet name, I am trying to write some code that returns TRUE if a
sheet exists and FALSE if it does not.

Ex: SheetName = "Bob", Returns TRUE if there is a sheet by this name,
returns FALSE if it does not.

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Does a sheet exist

You could use a function (from Chip Pearson) to test the existence of a sheet.

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

'and you can use it like:
....
if worksheetexists("myname",activeworkbook) then

========
Lots of times, I'll just check with this kind of code:

Dim wks as worksheet
....
set wks = nothing
on error resume next
set wks = worksheets("somesheetname")
on error goto 0
if wks is nothing then
'not there
else
'it is there
end if

Randall wrote:

Given a sheet name, I am trying to write some code that returns TRUE if a
sheet exists and FALSE if it does not.

Ex: SheetName = "Bob", Returns TRUE if there is a sheet by this name,
returns FALSE if it does not.

Thanks


--

Dave Peterson
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
If Sheet Exist Peter Excel Programming 4 August 23rd 06 02:00 PM
VBA: Make a new sheet if it doesn't exist PaulW Excel Discussion (Misc queries) 2 May 8th 06 03:45 PM
error when sheet doesn't exist... Ctech[_38_] Excel Programming 10 November 11th 05 01:08 PM
Does the sheet exist? Dr.Schwartz Excel Programming 1 August 25th 04 02:16 PM
Does sheet exist? Sean Evanovich Excel Programming 2 November 19th 03 02:30 PM


All times are GMT +1. The time now is 04:48 PM.

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"