Function to Say if File is Open

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In a cell D4 in my worksheet, I have the filepath and name of a file I wish to open, e.g, C:\Mike1\abc.xlsm

Could anyone give me VBA for a user defined function that I could use in say, cell E4, that would return TRUE if the file is already open in the current instance of Excel?

Thanks,

Mike
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Thanks Neil - looks just like what I want.

However, when I use it, it currently working - I open a workbook, and then do the test, but it returns false. At the moment my name to check has the full filepath and then the workbook name - no quote marks.

Do you know what I should have so that the right value of wbName is submitted?

Thanks,

Mike
 
Upvote 0
Sorry, line 2 should say "it is currently not working"
 
Upvote 0
Sorry, line 2 should say "it is currently not working"
 
Upvote 0
The function I pointed out doesn't consider the path. It tries to set a variable (X) to a workbook with the name you pass to it. If this returns an error, it means the workbook isn't open (or doesn't exist). If it doesn't return an error, then it's already open.
 
Upvote 0
The function I pointed out doesn't consider the path. It tries to set a variable (X) to a workbook with the name you pass to it. If this returns an error, it means the workbook isn't open (or doesn't exist). If it doesn't return an error, then it's already open.

Thanks,

I just created a workbook called Test.xlsm - I saved it and left it open. I then created another workbook and included the function and referred to cell A1 as the input. But no matter what I put in cell A1, the function still says FALSE. In A1 I have tried Test, Test.xlsm, "Test", and "Test.xlsm" with no luck.

I must be doing something wrong.
 
Upvote 0
If I change the code from:

Set x = Workbooks("wbname")

to: Set x = Workbooks("Test.xlsm")

it works.

Hmmmm
 
Upvote 0
You shouldn't change the function, just create a macro to call it i.e. Macro1 will test if the workbook "Test.xslm" is open in the same session (True) or not (False):

Code:
Option Explicit
Private Function WorkbookIsOpen(wbname) As Boolean
'   Returns TRUE if the workbook is open
    Dim x As Workbook
    On Error Resume Next
        Set x = Workbooks(wbname)
        If Err = 0 Then WorkbookIsOpen = True _
            Else WorkbookIsOpen = False
    On Error GoTo 0
End Function
Sub Macro1()

    WorkbookIsOpen ("Test.xlsm")

End Sub

HTH

Robert
 
Upvote 0
You shouldn't change the function, just create a macro to call it i.e. Macro1 will test if the workbook "Test.xslm" is open in the same session (True) or not (False):

Code:
Option Explicit
Private Function WorkbookIsOpen(wbname) As Boolean
'   Returns TRUE if the workbook is open
    Dim x As Workbook
    On Error Resume Next
        Set x = Workbooks(wbname)
        If Err = 0 Then WorkbookIsOpen = True _
            Else WorkbookIsOpen = False
    On Error GoTo 0
End Function
Sub Macro1()

    WorkbookIsOpen ("Test.xlsm")

End Sub

HTH

Robert

Thanks Robert,

I notice in your version, you have Set x = Workbooks(wbname)

whereas in the original, the code was Set x = Workbooks("wbname"). I will check, but maybe that's the issue.

MikeG
 
Upvote 0

Forum statistics

Threads
1,214,801
Messages
6,121,644
Members
449,045
Latest member
Marcus05

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