ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Test if sheet exits, create with name if not (https://www.excelbanter.com/excel-programming/314313-test-if-sheet-exits-create-name-if-not.html)

bramweisman[_8_]

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


Ron de Bruin

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




Bob Phillips[_6_]

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




Ron de Bruin

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






Don Guillett[_4_]

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





All times are GMT +1. The time now is 07:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com