Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default search for worksheet, insert new if doesn't exist


Im opening up a workbook and then selecting a worksheet based on
string variable like so (wsI is worksheet variable, sYear is searc
criteria)

Set wsI = Sheets(sYear)

I would like to improve this so that if the worksheet does not exist
insert a new one and then rename it to my search variable, i though
something along the lines of the below would work but it hasn't!
guess the 0 is the wrong thing to look for, any help appreciated

If Sheets(sYear) = 0 Then
Sheets.Add
ActiveSheet.Name = sYear
Else
Set wsI = Sheets(sYear)
End I

--
cereldin
-----------------------------------------------------------------------
cereldine's Profile: http://www.excelforum.com/member.php...fo&userid=3206
View this thread: http://www.excelforum.com/showthread.php?threadid=53500

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default search for worksheet, insert new if doesn't exist

Here is a function that tells you if a sheet exists in a given workbook

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

If sheetexists(sYear) Then
Set wsI = Sheets(sYear)
Else
Sheets.Add
Set wsI = Activesheet
wsI.Name = sYear
End If

--
HTH...

Jim Thomlinson


"cereldine" wrote:


Im opening up a workbook and then selecting a worksheet based on a
string variable like so (wsI is worksheet variable, sYear is search
criteria)

Set wsI = Sheets(sYear)

I would like to improve this so that if the worksheet does not exist i
insert a new one and then rename it to my search variable, i thought
something along the lines of the below would work but it hasn't! I
guess the 0 is the wrong thing to look for, any help appreciated

If Sheets(sYear) = 0 Then
Sheets.Add
ActiveSheet.Name = sYear
Else
Set wsI = Sheets(sYear)
End If


--
cereldine
------------------------------------------------------------------------
cereldine's Profile: http://www.excelforum.com/member.php...o&userid=32069
View this thread: http://www.excelforum.com/showthread...hreadid=535008


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default search for worksheet, insert new if doesn't exist

On Error Resume Next
Set wsI = Worksheets(sYear)
On Error GoTo 0
If wsI Is Nothing Then
Worksheets.Add.Name = sYear
Set wsI = Worksheets(sYear)
End If

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"cereldine" wrote
in message ...

Im opening up a workbook and then selecting a worksheet based on a
string variable like so (wsI is worksheet variable, sYear is search
criteria)

Set wsI = Sheets(sYear)

I would like to improve this so that if the worksheet does not exist i
insert a new one and then rename it to my search variable, i thought
something along the lines of the below would work but it hasn't! I
guess the 0 is the wrong thing to look for, any help appreciated

If Sheets(sYear) = 0 Then
Sheets.Add
ActiveSheet.Name = sYear
Else
Set wsI = Sheets(sYear)
End If


--
cereldine
------------------------------------------------------------------------
cereldine's Profile:

http://www.excelforum.com/member.php...o&userid=32069
View this thread: http://www.excelforum.com/showthread...hreadid=535008



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
search if cell contents exist in other cells CharileBaker Excel Worksheet Functions 1 April 12th 07 11:00 AM
File opens as worksheet.xls:1 and worksheet.xls:2. Only one exist Dave the wave[_2_] Excel Programming 5 January 30th 06 11:28 PM
Insert if not exist SG Excel Programming 4 October 24th 05 06:22 PM
How do i insert lines into a worksheet depending on if data exist. Nutter Excel Programming 0 February 9th 05 03:23 PM
search range, if exist, copy cell marcus76 Excel Programming 2 October 11th 04 06:01 PM


All times are GMT +1. The time now is 12:50 PM.

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"