Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Create a test | Excel Discussion (Misc queries) | |||
CREATE A LOGICAL TEST CONTAINING TEXT | Excel Worksheet Functions | |||
help - create a multiple choice test | Excel Discussion (Misc queries) | |||
Test if folder exists, create if it doesn't? | Excel Programming | |||
Test if a folder exists, create if it doesn't? | Excel Programming |