To check if a workbook is open, if it’s not open it

This Procedure will ask for a file name to the user, will check if the file is open and if it’s not open it will open the file otherwise give a message to the user.

Public Sub IsWorkbookOpen()
Dim Wb As Workbook, wkbk As Variant
‘get the dialog box to open the file
wkbk = Application.GetOpenFilename(“Excel Files,*.xls”)
If wkbk = False Then Exit Sub
‘check if its already opened in the Windows collection
Dim flag As Boolean
flag = False
For Each Wb In Workbooks
If Wb.Path & “\” & Wb.Name = wkbk Then
MsgBox “File is already open”, vbExclamation, “Workbook Open”
flag = True
End If
Next Wb
If flag = False Then Workbooks.Open (wkbk)
End Sub

About the Author

The author of Tableau Data Visualization Cookbook and an award winning keynote speaker, Ashutosh R. Nandeshwar is one of the few analytics professionals in the higher education industry who has developed analytical solutions for all stages of the student life cycle (from recruitment to giving). He enjoys speaking about the power of data, as well as ranting about data professionals who chase after “interesting” things. He earned his PhD/MS from West Virginia University and his BEng from Nagpur University, all in industrial engineering. Currently, he is leading the data science, reporting, and prospect development efforts at the University of Southern California.