Macro to see if a file is open
Power Query Course in Spanish
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 3 of 3

Thread: Macro to see if a file is open

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Yorkshire
    Posts
    213
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     

    I have a macro which opens a specific file. I would like to check to see if that file is open so that there is no error on running the macro.

    I would like something along the lines of

    If workbook x is open then
    activate workbook X else
    open workbook x
    end if

    Thanks

    Stewart


  2. #2
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Something like this would work. I don't know if there's anything more efficient though.


    Dim oWkBook As Workbook
    Dim bCheck As Boolean

    bCheck = False

    For Each oWkBook In Workbooks
    If UCase(oWkBook.FullName) = UCase("C:TEMPBook1.xls") Then
    bCheck = True
    Exit For
    End If
    Next

    If bCheck Then
    'Your code
    MsgBox "hi"
    End If


    HTH

  3. #3
    Board Regular
    Join Date
    Apr 2002
    Location
    A Mile High!!
    Posts
    241
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    This one works too. I like the other version as you can use a variable.

    If 'some code
    Workbooks.Open FileName:= _
    "\serverfoldersheet.xls", _
    updatelinks:=3, WriteResPassword:="pw"
    'check to see if read only
    If ActiveWorkbook.ReadOnly Then
    ActiveWorkbook.Saved = True
    ActiveWorkbook.Close
    end if

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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

 

 
DMCA.com