View Full Version : [SOLVED:] checking if some specific workbook is open; if not then it should be opened.
Cinema
12-21-2015, 03:04 AM
Hello,
I want to write a code that checks if some specific workbook (wb2) is open already and if not then it should open wb2.
Bob Phillips
12-21-2015, 05:46 AM
What is wb2 exactly? Is it a code object, your name for it, or what?
Cinema
12-21-2015, 06:54 AM
Hi xld,
my workbook is called wb2.csv
This is my code:
Sub Import()
Dim fname As String
Dim fpath As String
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = ThisWorkbook
fpath = wb1.Sheets(Sheet1).Range("Path").Value
fname = wb1.Sheets(Sheet1).Range("Name").Value
'Open Workbook
Application.DisplayAlerts = False
Set wb2 = Workbooks.Open(fpath & "\" & fname)
Application.DisplayAlerts = True
'code
'code
This opens wb2.csv But I want first check if wb2.csv is already open and if not it should be opened.
Paul_Hossler
12-21-2015, 08:29 AM
I think something like this
Sub CheckOpen()
Dim wb As Workbook, wbCSV As Workbook
Set wbCSV = Nothing
For Each wb In Application.Workbooks
If wb.Name = "wb2.csv" Then
Set wbCSV = wb
Exit For
End If
Next
If wbCSV Is Nothing Then
MsgBox "Open it"
Else
MsgBox "Already open"
End If
End Sub
Bob Phillips
12-21-2015, 04:34 PM
Another way
Dim isOpen As Boolean
On Error Resume Next
isOpen = CBool(Not Workbooks("wb2.csv") Is Nothing)
On Error Goto 0
If isOpen Then
MsgBox "Already open"
Else
MsgBox "Open it"
End If
Cinema
12-23-2015, 02:03 AM
This is perfect thank you !
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.