+ Reply to Thread
Results 1 to 4 of 4

Open workbook already open

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-18-2008
    Location
    uk
    MS-Off Ver
    Excel 2003 and Excel 2010(sometimes)
    Posts
    139

    Open workbook already open

    hi

    i have the following problem

    i have a userform which opens a file

    the problem is that the userform can opt to open the file again

    i can either close the file after use
    or
    enter some code which will determine whether the file is already open

    i can do the first bit but it isnt ideal

    the second part is the bit im stuff on

    any help would be appreciated

    thanks in advance

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Open workbook already open

    On Error Resume Next
    Set wb = Workbooks("C:\files\myworkbook.xls")
    On Error Goto 0
    
    If wb Is Nothing Then
    
    Set wb = Workbooks.Open "C:\files\myworkbook.xls"
    End If

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Open workbook already open

    Bob, given full file name would that work if the file were already open ?

    Slight variation on Bob's post:

    Sub Example()
    Dim wbEx As Workbook
    Const cPath = "C:\files\"
    Const cFileName = "myworkbook.xls"
    On Error Resume Next
    Set wbEx = Workbooks(cFileName)
    On Error GoTo 0
    If wbEx Is Nothing Then
        Workbooks.Open cPath & cFileName
    End If
    Set wbEx = Nothing
    End Sub

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Open workbook already open

    Another approach might be.
    Option Explicit
    
    Sub Example()
        Dim wbEx As Workbook
        Dim i As Integer
        
        Const cPath = "C:\files\"
        Const cFileName = "myworkbook.xls"
           
        For i = 1 To Application.Workbooks.Count
            If Application.Workbooks(i).Name = cFileName Then
                Set wbEx = Workbooks(cFileName)
                Exit For
            End If
        Next
        If wbEx Is Nothing Then
            If Dir(cPath & cFileName) <> "" Then
                Workbooks.Open cPath & cFileName
                Set wbEx = Workbooks(cFileName)
            Else
                MsgBox cPath & cFileName & Chr(10) & Chr(10) & "Could not be found"
                Exit Sub
            End If
        End If
        ' begin your code here
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1