ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   add sheet if statement (https://www.excelbanter.com/excel-programming/312878-add-sheet-if-statement.html)

scrabtree[_2_]

add sheet if statement
 
I want to run a certain VBA code if a sheet in a workbook
is present and not if it isn't. If there is a sheet
named "Time" I want it to do nothing. If there is no
sheet named "Time" I want it to add a sheet and name
it "Time".



SCSA25[_3_]

add sheet if statement
 

Function SheetExists(SheetName As String) As Boolean
' returns TRUE if the sheet exists in the active workbook
SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets(SheetName).Name) 0 Then
SheetExists = True
Exit Function
End If
NoSuchSheet:
End Functio

--
SCSA2
-----------------------------------------------------------------------
SCSA25's Profile: http://www.excelforum.com/member.php...fo&userid=1510
View this thread: http://www.excelforum.com/showthread.php?threadid=26740


Juan Pablo González

add sheet if statement
 
Try something like

Dim Sht As Worksheet
On Error Resume Next
Set Sht = ActiveWorkbook.Worksheets("Time")
On Error Goto 0
If Sht Is Nothing Then
Sheets.Add.Name = "Time"
End If

--
Regards

Juan Pablo González

"scrabtree" wrote in message
...
I want to run a certain VBA code if a sheet in a workbook
is present and not if it isn't. If there is a sheet
named "Time" I want it to do nothing. If there is no
sheet named "Time" I want it to add a sheet and name
it "Time".





Bob Phillips[_6_]

add sheet if statement
 
'-----------------------------------------------------------------
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

"scrabtree" wrote in message
...
I want to run a certain VBA code if a sheet in a workbook
is present and not if it isn't. If there is a sheet
named "Time" I want it to do nothing. If there is no
sheet named "Time" I want it to add a sheet and name
it "Time".






All times are GMT +1. The time now is 08:33 AM.

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