Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default To check worksheets are open or not

While working on various sheets some times i get a error message if the sheet
is not there i would like to check prior to activating it. Is there any
command available to check the sheets e.g. book7 or book9 etc.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default To check worksheets are open or not

Vijay,

Use a function like

Function SheetExists(SheetName As String, Optional WBook As Workbook) As
Boolean
On Error Resume Next
SheetExists = CBool(Len(IIf(WBook Is Nothing, _
ThisWorkbook, WBook).Sheets(SheetName).Name))
End Function

You can then call this as

If SheetExists("Sheet1") = True Then

or

If SheetExists("Sheet1", Workbooks("Book2.xls")) = True Then

If you omit the workbook reference, it tests for the sheet in the same
workbook that contains the code, as shown in the first example. To test for
a sheet in another workbook, include the workbook reference as shown in the
second example.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)



"Vijay Kotian" wrote in message
...
While working on various sheets some times i get a error message if the
sheet
is not there i would like to check prior to activating it. Is there any
command available to check the sheets e.g. book7 or book9 etc.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default To check worksheets are open or not

Sub wbtest()
Dim w As Workbook
Dim s As String
s = "Book"
For Each w In Workbooks
If w.Name = s Then
MsgBox (w.Name & " is open")
Exit Sub
End If
Next
MsgBox (s & " is not open")
End Sub


--
Gary's Student


"Vijay Kotian" wrote:

While working on various sheets some times i get a error message if the sheet
is not there i would like to check prior to activating it. Is there any
command available to check the sheets e.g. book7 or book9 etc.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default To check worksheets are open or not

The most efficient way is to trap the resulting error and process
accordingly, e.g.

On Error Resume Next
Sheets("book7").Activate
If Err.Number = 9 then
Msgbox "book7 not found"
End If
On Error Goto 0

If this is not desired then you will have to loop through all sheets in the
active workbook to see if one of them is named "book7"

Function WorkSheetExists(strName as String) As Boolean
Dim w as Object

For Each w In ActiveWorkbook.Sheets
If w.Name = strName Then
WorkSheetExists = True
End If
Next w
End Function

Cheers,
Dave
"Vijay Kotian" wrote:

While working on various sheets some times i get a error message if the sheet
is not there i would like to check prior to activating it. Is there any
command available to check the sheets e.g. book7 or book9 etc.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default To check worksheets are open or not

Hi,

I need to check only once (not loop) whether particular sheet is already
opened or not. I would like to use If else condition based on above
mentioned sheet. How can i do this.

I have tried your code and found that even if the file is not there is
proceeds instead of going to endif.

Pl. help.

Regards

"Dave Ramage" wrote:

The most efficient way is to trap the resulting error and process
accordingly, e.g.

On Error Resume Next
Sheets("book7").Activate
If Err.Number = 9 then
Msgbox "book7 not found"
End If
On Error Goto 0

If this is not desired then you will have to loop through all sheets in the
active workbook to see if one of them is named "book7"

Function WorkSheetExists(strName as String) As Boolean
Dim w as Object

For Each w In ActiveWorkbook.Sheets
If w.Name = strName Then
WorkSheetExists = True
End If
Next w
End Function

Cheers,
Dave
"Vijay Kotian" wrote:

While working on various sheets some times i get a error message if the sheet
is not there i would like to check prior to activating it. Is there any
command available to check the sheets e.g. book7 or book9 etc.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to check is someone else has a workbook open Jim K.[_2_] Excel Discussion (Misc queries) 2 August 29th 08 04:32 PM
check for worksheets erlanr Excel Worksheet Functions 1 September 8th 07 01:34 AM
Check if a document is open AnthonyJ Excel Programming 3 September 29th 06 03:15 PM
Check if workbook open dkipping Excel Discussion (Misc queries) 4 May 24th 06 02:00 PM
check if file already is open Mats Nilsson Excel Programming 2 September 12th 05 09:30 AM


All times are GMT +1. The time now is 03:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"