Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Test if sheet exists create if not

I am using Excel 2003 I am trying to test is a sheet exist and if not
create the sheet.

I have search the and have found and edited the following in a moduel.
I have been testing this but it is always returning False. Unless
filter_by = "Sheet1"

Function SheetExists(SheetName As String) As Boolean
On Error Resume Next
SheetExists = CBool(Len(ThisWorkbook.Worksheets(SheetName).Name) )
End Function

Sub test()
Dim filter_by As String
filter_by = "test"

If SheetExists(filter_by) = False Then
Worksheets.Add.Name = filter_by
Else
' do something else
End If

End Sub

--------------------------------------------------------
Thanks in advance

Sean

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Test if sheet exists create if not

It works just fine on Excel 2002 - in fact, I think I'll borrow your
bit of code and use it myself!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Test if sheet exists create if not

Glad I could help :)

Sean

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Test if sheet exists create if not

Try replacing
SheetExists = CBool(Len(ThisWorkbook.Worksheets(SheetName).Name) )

with
dim SheetExists as boolean
If Len(ThisWorkbook.Worksheets(SheetName).Name) 0 then
SheetExists = true
else
SheetExists = false
end if

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Test if sheet exists create if not

Again if I use

dim SheetExists as boolean
If Len(ThisWorkbook.Worksheets("Shhet1").Name) 0 then
SheetExists = true
else
SheetExists = false
end if

it works.

But if I use

dim SheetExists as boolean
If Len(ThisWorkbook.Worksheets("test").Name) 0 then
SheetExists = true
else
SheetExists = false
end if

I am get a runtime error 9 subscript out of range.


Sean



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Test if sheet exists create if not

To added some more detail.

I have a workbook where each tab is named with the customer name. I get
a list of customer names from a DB query and use:

Sheets("Customer_Array").Select
Cells(namerow, namecol).Select
filter_by = ActiveCell


what I need it the macro to add a new sheet with the customer name
atained from the filter_by variable it it does not already exist.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Test if sheet exists create if not

This change made the function worked,

Function SheetExists(SheetName As String) As Boolean
On Error Resume Next
SheetExists = Len(Sheets(SheetName).Name)
End Function

Sean

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
How to test Worksheet exists before trying to create it? CRayF Excel Programming 7 September 23rd 05 08:33 AM
If Sheet exists, GOTHERE, else create it CLR Excel Programming 3 March 29th 05 06:03 PM
Test if Sheet Exists - Tom Ogilvy Steph[_3_] Excel Programming 8 September 23rd 04 04:37 AM
Test if folder exists, create if it doesn't? 43fan Excel Programming 1 March 1st 04 04:31 PM
Test if a folder exists, create if it doesn't? 43fan Excel Programming 3 March 1st 04 02:59 PM


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