VBA How to Know if a Workbook is opened?

jamiguel77

Active Member
Joined
Feb 14, 2006
Messages
387
Office Version
  1. 2016
  2. 2010
  3. 2007
Platform
  1. Windows
  2. Web
hi all

i have this VBA code:


Dim wbk As Workbook
Dim wbk2 As Workbook

Set wbk2 = Workbooks.Open(Filename:=F2)
Set wbk = Workbooks.Open(Filename:=F1)

of course F1 and F2 is a Correct filenames with path.....

My question is how to know if workbook: wbk is already opened? (for not reopen....) and of course assign to variable something like this:

if (Workbooks.Open(Filename:=F1) = opened ) then

else
Workbooks.Open(Filename:=F1)
endif

thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
yes, i see, and know but this time i need know if workbook was opened before..

how to?

Thanks
 
Upvote 0
You could have adapted my solution there to this query?

Anyway, try this where I've included code to make sure the file exists first:

Code:
Option Explicit
Sub Macro2()

    Dim blnFileExists As Boolean
    Dim objFSO As Object
    Dim strFullPath As String
    Dim wbk As Workbook
    
    Application.ScreenUpdating = False
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    'Full path (directory + filename) to be checked for existence. Change to suit.
    strFullPath = "C:\Users\Documents\Test File.xlsx"
    blnFileExists = objFSO.FileExists(strFullPath)
    
    'If the file exists, then...
    If blnFileExists = True Then
        On Error Resume Next
            '...see if it's open in the current Excel session
            Set wbk = Workbooks(objFSO.GetFileName(strFullPath))
            'If the file is not open, then...
            If Err.Number <> 0 Then
                '...open it
                Set wbk = Workbooks.Open(strFullPath)
            End If
        On Error GoTo 0
    End If
    
    Application.ScreenUpdating = True

End Sub

You could put each filename in an array and then check each item in that array with my code above.

Regards,

Robert
 
Upvote 0
Thanks.

Solved my Problem:

Declared the variables wbk and wbk2 as public... and i can use the variable in my program.

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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