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 exits, create with name if not


I have some code that successfully imports data into worksheets. I hav
a variable HOTSHEET that contains the text string from a certain cell.
How can I test to see if this sheet exits, and if not create one wit
the proper name. I saw Tom Oglvy's response to Steph on a simila
subject but couldn't really make sense of it in this context

--
bramweisma
-----------------------------------------------------------------------
bramweisman's Profile: http://www.excelforum.com/member.php...nfo&userid=174
View this thread: http://www.excelforum.com/showthread.php?threadid=27128

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Test if sheet exits, create with name if not

Hi Bram

Copy the function and the macro in a normal module

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

Sub Test()
Dim HOTSHEET As String
HOTSHEET = "test"
If SheetExists(HOTSHEET) = False Then
Sheets.Add.Name = "test"
Else
'do nothing
End If
End Sub




--
Regards Ron de Bruin
http://www.rondebruin.nl


"bramweisman" wrote in message ...

I have some code that successfully imports data into worksheets. I have
a variable HOTSHEET that contains the text string from a certain cell.
How can I test to see if this sheet exits, and if not create one with
the proper name. I saw Tom Oglvy's response to Steph on a similar
subject but couldn't really make sense of it in this context.


--
bramweisman
------------------------------------------------------------------------
bramweisman's Profile: http://www.excelforum.com/member.php...fo&userid=1746
View this thread: http://www.excelforum.com/showthread...hreadid=271289



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Test if sheet exits, create with name if not

One way

Sub CreateIt()

If Not SheetExists("mySheet") Then
Worksheets.Add.Name = "mySheet"
End If

End Sub

'-----------------------------------------------------------------
Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function



--

HTH

RP

"bramweisman" wrote in message
...

I have some code that successfully imports data into worksheets. I have
a variable HOTSHEET that contains the text string from a certain cell.
How can I test to see if this sheet exits, and if not create one with
the proper name. I saw Tom Oglvy's response to Steph on a similar
subject but couldn't really make sense of it in this context.


--
bramweisman
------------------------------------------------------------------------
bramweisman's Profile:

http://www.excelforum.com/member.php...fo&userid=1746
View this thread: http://www.excelforum.com/showthread...hreadid=271289



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Test if sheet exits, create with name if not

Oops

Use this

Sheets.Add.Name = HOTSHEET


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ron de Bruin" wrote in message ...
Hi Bram

Copy the function and the macro in a normal module

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

Sub Test()
Dim HOTSHEET As String
HOTSHEET = "test"
If SheetExists(HOTSHEET) = False Then
Sheets.Add.Name = "test"
Else
'do nothing
End If
End Sub




--
Regards Ron de Bruin
http://www.rondebruin.nl


"bramweisman" wrote in message ...

I have some code that successfully imports data into worksheets. I have
a variable HOTSHEET that contains the text string from a certain cell.
How can I test to see if this sheet exits, and if not create one with
the proper name. I saw Tom Oglvy's response to Steph on a similar
subject but couldn't really make sense of it in this context.


--
bramweisman
------------------------------------------------------------------------
bramweisman's Profile: http://www.excelforum.com/member.php...fo&userid=1746
View this thread: http://www.excelforum.com/showthread...hreadid=271289





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default Test if sheet exits, create with name if not

try this. It will make it if if does not exist.

Sub namenewsheet()
Dim ns As Worksheet
myname = "mynewsheetname"
'myname=range("d1").value
On Error Resume Next
Set ns = ActiveWorkbook.Worksheets(myname)
On Error GoTo 0
If sht Is Nothing Then Sheets.Add.Name = myname
End Sub

--
Don Guillett
SalesAid Software

"bramweisman" wrote in message
...

I have some code that successfully imports data into worksheets. I have
a variable HOTSHEET that contains the text string from a certain cell.
How can I test to see if this sheet exits, and if not create one with
the proper name. I saw Tom Oglvy's response to Steph on a similar
subject but couldn't really make sense of it in this context.


--
bramweisman
------------------------------------------------------------------------
bramweisman's Profile:

http://www.excelforum.com/member.php...fo&userid=1746
View this thread: http://www.excelforum.com/showthread...hreadid=271289



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
Create a test skemeny Excel Discussion (Misc queries) 1 December 14th 06 03:57 PM
CREATE A LOGICAL TEST CONTAINING TEXT help needed Excel Worksheet Functions 2 August 17th 06 03:00 PM
help - create a multiple choice test [email protected] Excel Discussion (Misc queries) 2 July 17th 06 12:31 PM
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 07:33 AM.

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"