Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
If Sheet Exist | Excel Programming | |||
VBA: Make a new sheet if it doesn't exist | Excel Discussion (Misc queries) | |||
error when sheet doesn't exist... | Excel Programming | |||
Does the sheet exist? | Excel Programming | |||
Does sheet exist? | Excel Programming |