Check if File is Open

taltyr

New Member
Joined
Jul 20, 2011
Messages
31
On the current Workbook I have a button, it runs the code below.

Ideally the user would be given the instruction to select the latest "ECHIT" workbook. If it is not open, Excel will open it, refresh a Pivot Table and copy the data back into the current Workbook. If it is open, Excel should activate that open file and do the same refresh, copy, paste exercise.

I have managed to get most of it to work, it is just the file open/ not open alternatives that I have difficulty with.

The code I have so far is;

Code:
Sub UpdateFile()
Dim strThisWorkbook
'Dim ECHIT As String
strThisWorkbook = ThisWorkbook.Name
MsgBox "Select up-to-date ECHIT Schedule"
ECHIT = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Please select a file")
If ECHIT = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file"
Exit Sub
Else
    If Not IsFileOpen("ECHIT") Then
        Workbooks.Open FileName:=ECHIT
 
        Else
        Workbooks("ECHIT").Activate
 
    End If
'Workbooks.Open FileName:=ECHIT
'MsgBox ECHIT
   ' Workbooks(ECHIT).Activate
    'Workbooks.Activate Filename:=ECHIT
    Sheets("EXPORT").Select
    ActiveSheet.PivotTables("PivotTable1").PivotSelect _
        "'Budget aligned Contract Number'[All]", xlLabelOnly + xlFirstRow, True
    Application.CutCopyMode = False
    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    Cells.Select
    Selection.Copy
    'ActiveWindow.Close
    Windows(strThisWorkbook).Activate
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End If
End Sub
Function IsFileOpen(FileName As String)
    Dim iFilenum As Long
    Dim iErr As Long
 
    On Error Resume Next
    iFilenum = FreeFile()
    Open FileName For Input Lock Read As #iFilenum
    Close iFilenum
    iErr = Err
    On Error GoTo 0
 
    Select Case iErr
    Case 0:    IsFileOpen = False
    Case 70:   IsFileOpen = True
    Case Else: Error iErr
    End Select
 
End Function

As ever any help greatly appreciated.

taltyr
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Good Question, hadn't thought of that.

Now that you ask, in an ideal world, what I would like is;

If it is open on another computer I would get a Yes/ No Box Message asking me if it was OK to use that file already open file on another users PC.

AND

If it is open on my computer I would get a Yes/ No Box Message asking me if it was OK to use that already open file on my PC.

Bit of an expanded question I know, but like I say I hadn't thought of it.

Regards

Taltyr
 
Upvote 0
The function you have is fine - you just need to change this bit:

Code:
If Not IsFileOpen("ECHIT") Then   
        Workbooks.Open FileName:=ECHIT
 
        Else
        Workbooks("ECHIT").Activate
 
    End If

to this:

Code:
Dim wb As Workbook

If Not IsFileOpen(ECHIT) Then    '=====Note no quotes!
        Workbooks.Open FileName:=ECHIT
 
        Else
            On Error Resume Next
            Set wb = Workbooks(Mid(ECHIT,InstrRev(ECHIT,"\")+1))
            On Error Goto 0
            If wb Is Nothing Then
                 If MsgBox("ECHIT workbook open on another computer or in another Excel session on this computer! Open it up anyway?",vbYesNo) = vbYes Then
                       Set wb = Workbooks.Open(Filename:=ECHIT,ReadOnly=True)
                 Else
                     Exit Sub
                 End If
            Else
                  Workbooks(Mid(ECHIT,InstrRev(ECHIT,"\")+1)).Activate
            End if
 
    End If
 
Last edited:
Upvote 0
Thanks so far.

When I run it I get a Compile error: ByRef argument type mismatch on the 1st line

Code:
If Not IsFileOpen(ECHIT) Then    '=====Note no quotes!

The line
Code:
Set wb = Workbooks.Open(Filename:=ECHIT,ReadOnly=True)

is also showing in red but not sure what the error is there.

Regards

Talty
 
Upvote 0
Change the definition of the function (1st line) to:

Rich (BB code):
Function IsFileOpen(ByVal FileName As String)

amendment in red.

Change that other line to:

Rich (BB code):
Set wb = Workbooks.Open(Filename:=ECHIT,ReadOnly:=True)

also in red
 
Upvote 0
For some reason it does not pick up that the file is open and skips to the Else bit of the If statement.

To be sure, this is the code I now have;

Code:
Sub UpdateFile()
Dim strThisWorkbook
'Dim ECHIT As String
Dim wb As Workbook
strThisWorkbook = ThisWorkbook.Name
MsgBox "Select up-to-date ECHIT Schedule"
ECHIT = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Please select a file")
If ECHIT = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file"
Exit Sub
Else

If Not IsFileOpen(ECHIT) Then    '=====Note no quotes!
        Workbooks.Open FileName:=ECHIT
 
        Else
            On Error Resume Next
            Set wb = Workbooks(Mid(ECHIT, InStrRev(ECHIT, "\") + 1))
            On Error GoTo 0
            If wb Is Nothing Then
                 If MsgBox("ECHIT workbook open on another computer or in another Excel session on this computer! Open it up anyway?", vbYesNo) = vbYes Then
                       Set wb = Workbooks.Open(FileName:=ECHIT, ReadOnly:=True)
                 Else
                     Exit Sub
                 End If
            Else
                  Workbooks(Mid(ECHIT, InStrRev(ECHIT, "\") + 1)).Activate
            End If
 
    End If
'Workbooks.Open FileName:=ECHIT
'MsgBox ECHIT
   ' Workbooks(ECHIT).Activate
    'Workbooks.Activate Filename:=ECHIT
    Sheets("EXPORT").Select
    ActiveSheet.PivotTables("PivotTable1").PivotSelect _
        "'Budget aligned Contract Number'[All]", xlLabelOnly + xlFirstRow, True
    Application.CutCopyMode = False
    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    Cells.Select
    Selection.Copy
    'ActiveWindow.Close
    Windows(strThisWorkbook).Activate
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End If
End Sub
Function IsFileOpen(ByVal FileName As String)
    Dim iFilenum As Long
    Dim iErr As Long
     
    On Error Resume Next
    iFilenum = FreeFile()
    Open FileName For Input Lock Read As #iFilenum
    Close iFilenum
    iErr = Err
    On Error GoTo 0
     
    Select Case iErr
    Case 0:    IsFileOpen = False
    Case 70:   IsFileOpen = True
    Case Else: Error iErr
    End Select
     
End Function
 
Upvote 0
I have it open in the same place as the current Workbook (on my Laptop from My Documents).

I see that, on going through the function, it does select Case 70 as expected.


Regards

taltyr
 
Upvote 0
It worked for me as I expected it to - if the workbook was open in the same Excel session as that running the code, the workbook was activated. Have you turned off ScreenUpdating? That may prevent you seeing that this has happened.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top