ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Does a worksheet exist? (https://www.excelbanter.com/excel-programming/349617-does-worksheet-exist.html)

papa jonah

Does a worksheet exist?
 
I am trying to figure out what sort of code I can use in my vba that
would allow it to recognize whether a worksheet with a particular name
exists or not. I would like to use it in conjunction with an if-then
so that it will do one thing or another dependent on the presence (or
lack of) this sheet.

TIA


Bob Phillips[_6_]

Does a worksheet exist?
 
Use this function


'-----------------------------------------------------------------
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
(remove nothere from the email address if mailing direct)


"papa jonah" wrote in message
oups.com...
I am trying to figure out what sort of code I can use in my vba that
would allow it to recognize whether a worksheet with a particular name
exists or not. I would like to use it in conjunction with an if-then
so that it will do one thing or another dependent on the presence (or
lack of) this sheet.

TIA




Jim Thomlinson[_5_]

Does a worksheet exist?
 
Here is a function...

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
--
HTH...

Jim Thomlinson


"papa jonah" wrote:

I am trying to figure out what sort of code I can use in my vba that
would allow it to recognize whether a worksheet with a particular name
exists or not. I would like to use it in conjunction with an if-then
so that it will do one thing or another dependent on the presence (or
lack of) this sheet.

TIA




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

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