[RESOLVED] Check for open workbooks
Hi
Is there any way I can check if a specific workbook, e.g. book1.xls, is open?
I've been trying to do something like this;
VB Code:
If book.xls exist then 'This is the line that bothers me!
'code
else
workbooks.add 'creating book1.xls
'code
end if
I know i've posted a smiliar question earlier, but hopefully by rephraising it someone can help me :confused:
Thanx
Nick
Re: Check for open workbooks
VB Code:
Sub nextsub()
Dim b As Workbook
For Each b In Application.Workbooks
If b.Name = "Full structre practice.xls" Then
b.Activate
Exit Sub
End If
Next
Workbooks.Open "Full structre practice.xls"
Exit Sub
here is the code you need, it checks all the open workbooks till it finds the one you want, then maces it the active one, if not open, opens it
pete
Re: Check for open workbooks
I think you would be much better off with a generic function that accepts a bookname as a paramater and returns whether or not that book is open.
Somethin like..
VB Code:
Function BookIsOpen(ByRef BookName As String) As Boolean
Dim wkbBook As Workbook
For Each wkbBook In Application.Workbooks
If wkbBook.Name = BookName Then
BookIsOpen = True
Exit Function
End If
Next wkbBook
End Function
Then your code would look like
VB Code:
If BookIsOpen("YourBooksName") Then
'code
Else
Workbooks.Add 'creating book1.xls
'code
End If