ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   checking if a sheet exists (https://www.excelbanter.com/excel-programming/413802-checking-if-sheet-exists.html)

greg

checking if a sheet exists
 
Is there a good way of checking if a sheet exists?
Besides doing this
excel.ActiveWorkbook.Sheets("foo").name
and catching the error that is thrown.



Dave Peterson

checking if a sheet exists
 
I think that's the best way I've seen.

But if you don't like, you could loop through all the sheets to see if any of
the names match the name you're looking for. That seems less efficient than
what you suggest.

greg wrote:

Is there a good way of checking if a sheet exists?
Besides doing this
excel.ActiveWorkbook.Sheets("foo").name
and catching the error that is thrown.


--

Dave Peterson

Tom Ogilvy

checking if a sheet exists
 
looping through the sheets and checking each name



--
Regards,
Tom Ogilvy


"greg" wrote:

Is there a good way of checking if a sheet exists?
Besides doing this
excel.ActiveWorkbook.Sheets("foo").name
and catching the error that is thrown.




Gary Brown[_4_]

checking if a sheet exists
 
That is the best way as far as I can see....
'/===============================/
Public Function FileExists(strFileName As String) _
As Boolean
FileExists = False
If Dir(strFileName) < "" Then
FileExists = True
End If
End Function
'/===============================/

--
Hope this helps.
If this post was helpfull, please remember to click on the ''''YES''''
button at the bottom of the screen.
Thanks,
Gary Brown


"greg" wrote:

Is there a good way of checking if a sheet exists?
Besides doing this
excel.ActiveWorkbook.Sheets("foo").name
and catching the error that is thrown.




Gary Brown[_4_]

checking if a sheet exists
 
oops read it wrong. You said sheet, I thought file :O<
'==============================================
Public Function SheetExists(sName) As Boolean
' Returns TRUE if sheet exists in the active workbook
Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Sheets(sName)
If Err = 0 Then SheetExists = True _
Else SheetExists = False
End Function
'==============================================

--
Hope this helps.
If this post was helpfull, please remember to click on the ''''YES''''
button at the bottom of the screen.
Thanks,
Gary Brown


"Gary Brown" wrote:

That is the best way as far as I can see....
'/===============================/
Public Function FileExists(strFileName As String) _
As Boolean
FileExists = False
If Dir(strFileName) < "" Then
FileExists = True
End If
End Function
'/===============================/

--
Hope this helps.
If this post was helpfull, please remember to click on the ''''YES''''
button at the bottom of the screen.
Thanks,
Gary Brown


"greg" wrote:

Is there a good way of checking if a sheet exists?
Besides doing this
excel.ActiveWorkbook.Sheets("foo").name
and catching the error that is thrown.




Charlie

checking if a sheet exists
 
That's how I do it but you could do something like:

Public Function SheetExists(SheetName As String) As Boolean
'
Dim Sheet As Worksheet
'
For Each Sheet In ThisWorkbook.Worksheets
If Sheet.Name = SheetName Then
SheetExists = Yes
Exit Function
End If
Next Sheet
'
End Function


"greg" wrote:

Is there a good way of checking if a sheet exists?
Besides doing this
excel.ActiveWorkbook.Sheets("foo").name
and catching the error that is thrown.




Ron de Bruin

checking if a sheet exists
 
There are a few ways on this page
http://www.rondebruin.nl/exist.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"greg" wrote in message ...
Is there a good way of checking if a sheet exists?
Besides doing this
excel.ActiveWorkbook.Sheets("foo").name
and catching the error that is thrown.




All times are GMT +1. The time now is 06:48 AM.

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