ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Detecting if a Window/Workbook is Open (https://www.excelbanter.com/excel-discussion-misc-queries/149657-detecting-if-window-workbook-open.html)

Rob

Detecting if a Window/Workbook is Open
 
I was wondering how I would have a macro detect whether or not a workbook is
open.

This is what I tried.......
If Application.Windows("Book1.xls") = True Then

Obviously it doesn't work but does someone know what would?

Thanks Much.
R

Barb Reinhardt

Detecting if a Window/Workbook is Open
 
How about something like this

Sub Test()
Dim myWB As Workbook
Dim myString As String

myString = "Book2"
Set myWB = Nothing
On Error Resume Next
Set myWB = Workbooks(myString)
On Error GoTo 0
If Not myWB Is Nothing Then
Debug.Print myString & " is open"
Else
Debug.Print myString & " is not open"
End If

End Sub

HTH,
Barb Reinhardt

"Rob" wrote:

I was wondering how I would have a macro detect whether or not a workbook is
open.

This is what I tried.......
If Application.Windows("Book1.xls") = True Then

Obviously it doesn't work but does someone know what would?

Thanks Much.
R


Ron de Bruin

Detecting if a Window/Workbook is Open
 
I like to use a function

Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function


You can use this in your macro then

If bIsBookOpen("Book1.xls") Then



--

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


"Rob" wrote in message ...
I was wondering how I would have a macro detect whether or not a workbook is
open.

This is what I tried.......
If Application.Windows("Book1.xls") = True Then

Obviously it doesn't work but does someone know what would?

Thanks Much.
R


Mike H

Detecting if a Window/Workbook is Open
 
Try:-

Sub marine()
For Each wb In Workbooks
If wb.Name = ("mybook.xls") Then
MsgBox (wb.Name & " is open")
End If
Next wb
End Sub

Mike

"Rob" wrote:

I was wondering how I would have a macro detect whether or not a workbook is
open.

This is what I tried.......
If Application.Windows("Book1.xls") = True Then

Obviously it doesn't work but does someone know what would?

Thanks Much.
R


Rob

Detecting if a Window/Workbook is Open
 
They All Work for what I want to do... Now I just have to figure out which
one to use.

THANK YOU ALL!!!

R

"Rob" wrote:

I was wondering how I would have a macro detect whether or not a workbook is
open.

This is what I tried.......
If Application.Windows("Book1.xls") = True Then

Obviously it doesn't work but does someone know what would?

Thanks Much.
R


driller

Detecting if a Window/Workbook is Open
 
Hi Rob,

Good Point! Sometimes people like you have more luck than others !

regards,
driller
--
*****
birds of the same feather flock together..



"Rob" wrote:

They All Work for what I want to do... Now I just have to figure out which
one to use.

THANK YOU ALL!!!

R

"Rob" wrote:

I was wondering how I would have a macro detect whether or not a workbook is
open.

This is what I tried.......
If Application.Windows("Book1.xls") = True Then

Obviously it doesn't work but does someone know what would?

Thanks Much.
R



All times are GMT +1. The time now is 11:50 PM.

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