Results 1 to 4 of 4
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exists in collection? (WinNT4, XL97)

    Is there a way to get VBA to check if a particular workbook is open? I was thinking of exists in the collection, or 'isopen' or something, but I haven't been able to find anything that sounds like its what I want?
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exists in collection? (WinNT4, XL97)

    One way is to loop through the collection of workbooks:

    Function IsOpen(WorkbookName) As Boolean
    Dim wbk As Workbook
    For Each wbk In Workbooks
    If wbk.Name = WorkbookName Then
    IsOpen = True
    Exit For
    End If
    Next wbk
    Set wbk = Nothing
    End Function

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Exists in collection? (WinNT4, XL97)

    Thanks, Hans, that's just the job - I shall have to keep a copy of that little gem somewhere safe, I've wanted to do something like that so many times before but always had to find another way to do it because I could never work out a way to find out!

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    Beryl M


  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exists in collection? (WinNT4, XL97)

    Here is a generic one (courtesy Bill Manville):

    <pre>Function funIsIn(vCollection As Variant, sName As String) As Boolean
    Dim oObj As Object
    On Error Resume Next
    Set oObj = vCollection(sName)
    If oObj Is Nothing Then
    funIsIn = False
    Else
    funIsIn = True
    End If
    End Function

    Sub test()
    MsgBox funIsIn(Workbooks, "Book2")
    End Sub
    </pre>

    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

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