Excel VBA Test if workbook is open if so bring to the front

healey21

Well-known Member
Joined
Dec 22, 2009
Messages
900
I sometimes open a lot of workbooks and want to work on a specific workbook if it is open if not then I need to open it, if it is open how can I make that the active window workbook I am trying this out but can't get it switch windows if it is open or open if it is closed.

Sub isFileOpen()
Dim wkBk As Workbook

Set wkBk = Nothing
On Error Resume Next
Set wkBk = Workbooks("Invoice number vba.xlsm")
On Error GoTo 0

If wkBk Is Nothing Then
MsgBox "The File is not open!"
Else
wkBk = Workbooks.Open("L:\Invoice number vba.xlsm")
MsgBox "The File is open!"
End If

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try this:
Code:
Sub isFileOpen()
Dim wkBk As Workbook
If WorkbookOpen("Invoice number vba.xlsm") Then
    MsgBox "The File is open!"
Else
    Set wkBk = Workbooks.Open("L:\Invoice number vba.xlsm")
End If
End Sub
Function WorkbookOpen(WorkBookName As String) As Boolean
    WorkbookOpen = False
    On Error GoTo WorkBookNotOpen
    If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
        WorkbookOpen = True
        Exit Function
    End If
WorkBookNotOpen:
End Function
 
Upvote 0
Thanks Joe

This works if the workbook is closed, but doesn't bring to the front if it is already open, and I am not sure what needs to adjusted this maybe the 3rd book I have open or maybe 10th, Is there away to bring it to the front to be the activeworkbook rather than just using Ctrl + Tab until it appears?
 
Upvote 0
Try adding following line:
Rich (BB code):
Sub isFileOpen()
Dim wkBk As Workbook
If WorkbookOpen("Invoice number vba.xlsm") Then
    MsgBox "The File is open!"
    Workbooks("L:\Invoice number vba.xlsm").Activate
Else
    Set wkBk = Workbooks.Open("L:\Invoice number vba.xlsm")
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,674
Members
449,463
Latest member
Jojomen56

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