Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default problem finding named sheet

I have created a sheet using VBA. When I try to select the sheet, it doesn't
work, but if I try to name anew sheet with same name, an error occurs.

This code was originally used to create sheet (Dept is a string)
'create new dept sheet
Set NewSheet = Worksheets.Add
NewSheet.Name = Dept

This code is trying to test for presence of sheet, but testpagevalue does
not pick up value in the existing sheet, so gives error?

Private Function SheetExists(sname) As Boolean
' True if sheet exists in the active workbook and create if not present
Dim testPageValue As Variant 'use to test for page presence

'test if destination sheet exists
On Error Resume Next
Err.Clear
' any cell will do
testPageValue = Worksheets(sname).Range("A1").Value
If Err.Number < 0 Then
'page does not exist, create it
SheetExists = False
Err.Clear
On Error GoTo 0
Worksheets.Add ' add sheet, it gets selected
'can fail if destSheet is not a valid sheet name!
ActiveSheet.Name = sname ' name it
Else
SheetExists = True
End If
End Function



--
Patricia D
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default problem finding named sheet

Here is a sheet exists function that you can use...

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

Note that it does not create the sheet if one does not exist. That should be
done outside of the function within a sub procedure. Generally speaking (IMO)
in VBA functions should only return values. They should not create sheets or
other such side effects.

If sheetsexists("dept") = false then
'create your sheet
end if

--
HTH...

Jim Thomlinson


"Patricia D" wrote:

I have created a sheet using VBA. When I try to select the sheet, it doesn't
work, but if I try to name anew sheet with same name, an error occurs.

This code was originally used to create sheet (Dept is a string)
'create new dept sheet
Set NewSheet = Worksheets.Add
NewSheet.Name = Dept

This code is trying to test for presence of sheet, but testpagevalue does
not pick up value in the existing sheet, so gives error?

Private Function SheetExists(sname) As Boolean
' True if sheet exists in the active workbook and create if not present
Dim testPageValue As Variant 'use to test for page presence

'test if destination sheet exists
On Error Resume Next
Err.Clear
' any cell will do
testPageValue = Worksheets(sname).Range("A1").Value
If Err.Number < 0 Then
'page does not exist, create it
SheetExists = False
Err.Clear
On Error GoTo 0
Worksheets.Add ' add sheet, it gets selected
'can fail if destSheet is not a valid sheet name!
ActiveSheet.Name = sname ' name it
Else
SheetExists = True
End If
End Function



--
Patricia D

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
FINDING A NAMED TAB twinpat Excel Discussion (Misc queries) 4 May 19th 09 07:21 PM
Finding Named Field in Formulas Jeff Jones Excel Worksheet Functions 2 April 28th 07 01:22 PM
Finding Info from sheet 1 and removing only those rows from sheet Johnny B[_2_] Excel Discussion (Misc queries) 1 March 28th 07 02:29 PM
Create a sheet name, won't let me, says already sheet named that Buckwheat Excel Worksheet Functions 7 May 2nd 05 12:55 AM
referencing a sheet named in a cell then using data from that sheet gbeard Excel Worksheet Functions 4 April 15th 05 08:42 AM


All times are GMT +1. The time now is 09:11 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"