Results 1 to 3 of 3

Thread: [RESOLVED] Check for open workbooks

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Resolved [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:
    1. If book.xls exist then 'This is the line that bothers me!
    2. 'code
    3. else
    4. workbooks.add 'creating book1.xls
    5. 'code
    6. end if

    I know i've posted a smiliar question earlier, but hopefully by rephraising it someone can help me

    Thanx
    Nick

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Check for open workbooks

    VB Code:
    1. Sub nextsub()
    2. Dim b As Workbook
    3. For Each b In Application.Workbooks
    4. If b.Name = "Full structre practice.xls" Then
    5. b.Activate
    6. Exit Sub
    7. End If
    8. Next
    9. Workbooks.Open "Full structre practice.xls"
    10.  
    11. 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

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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:
    1. Function BookIsOpen(ByRef BookName As String) As Boolean
    2. Dim wkbBook As Workbook
    3.      
    4.      For Each wkbBook In Application.Workbooks
    5.         If wkbBook.Name = BookName Then
    6.             BookIsOpen = True
    7.             Exit Function
    8.         End If
    9.      Next wkbBook
    10. End Function

    Then your code would look like
    VB Code:
    1. If BookIsOpen("YourBooksName") Then
    2. 'code
    3. Else
    4. Workbooks.Add 'creating book1.xls
    5. 'code
    6. End If
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width