Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
dim dim is offline
external usenet poster
 
Posts: 123
Default Test if Workbook is already open ?

Hi all,

I've run into an unforseen hiccup in my code. I want to test if a workbook
is already open so that I don't try to open it again! In one area I open a
workbook called 3.xls using buttons in my main book called 73.xls.

I want to use a statement, so that if 3.xls is already open, I won't execute
the code Workbooks.Open section. I'm sure its a simple IF THEN ELSE, but with
my effort below I'm getting an "Invalid or Unqualifed Reference" error on the
first line when I try to re-execute the code with 3.xls already open. I
received no error when 3.xls was not open.

I dont have to use an IF statement, one it works I'll be happy....

Thanks

Private Sub CommandButton6_Click()
Application.ScreenUpdating = False
UserForm4.Hide
If Workbooks("3.xls") = .Open Then
Windows("73.xls").Activate
Sheets("CurrentEmployees").Select
Application.ScreenUpdating = True
Else
Workbooks.Open Filename:= _
"C:\Program Files\systems\MyProgram\Data1\3.xls"
Windows("73.xls").Activate
Sheets("CurrentEmployees").Select
Application.ScreenUpdating = True
End If

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default Test if Workbook is already open ?

On Jan 14, 1:06 am, dim wrote:
Hi all,

I've run into an unforseen hiccup in my code. I want to test if a workbook
is already open so that I don't try to open it again! In one area I open a
workbook called 3.xls using buttons in my main book called 73.xls.

I want to use a statement, so that if 3.xls is already open, I won't execute
the code Workbooks.Open section. I'm sure its a simple IF THEN ELSE, but with
my effort below I'm getting an "Invalid or Unqualifed Reference" error on the
first line when I try to re-execute the code with 3.xls already open. I
received no error when 3.xls was not open.

I dont have to use an IF statement, one it works I'll be happy....

Thanks

Private Sub CommandButton6_Click()
Application.ScreenUpdating = False
UserForm4.Hide
If Workbooks("3.xls") = .Open Then
Windows("73.xls").Activate
Sheets("CurrentEmployees").Select
Application.ScreenUpdating = True
Else
Workbooks.Open Filename:= _
"C:\Program Files\systems\MyProgram\Data1\3.xls"
Windows("73.xls").Activate
Sheets("CurrentEmployees").Select
Application.ScreenUpdating = True
End If

End Sub


One way...

Add this function to your project then call it up in your Sub to
determine if the workbook is open or not...

Public Function IsWorkbookOpen(stName As String) As Boolean
'IsWorkbookOpen returns True if stName is a member
'of the Workbooks collection. Otherwise it returns False
'stName must be provided as a filename without path

Dim Wkb As Workbook

On Error Resume Next
Set Wkb = Workbooks(stName)
If Not Wkb Is Nothing Then
IsWorkbookOpen = True
End If
End Function

Function straight from John Green's "Excel 2000 VBA Programmer's
Reference"

Ken Johnson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Test if Workbook is already open ?

I've run into an unforseen hiccup in my code. I want to test if a
workbook
is already open so that I don't try to open it again! In one area I open
a
workbook called 3.xls using buttons in my main book called 73.xls.

I want to use a statement, so that if 3.xls is already open, I won't
execute
the code Workbooks.Open section. I'm sure its a simple IF THEN ELSE, but
with
my effort below I'm getting an "Invalid or Unqualifed Reference" error on
the
first line when I try to re-execute the code with 3.xls already open. I
received no error when 3.xls was not open.


One way...

Add this function to your project then call it up in your Sub to
determine if the workbook is open or not...

Public Function IsWorkbookOpen(stName As String) As Boolean
'IsWorkbookOpen returns True if stName is a member
'of the Workbooks collection. Otherwise it returns False
'stName must be provided as a filename without path

Dim Wkb As Workbook

On Error Resume Next
Set Wkb = Workbooks(stName)
If Not Wkb Is Nothing Then
IsWorkbookOpen = True
End If
End Function

Function straight from John Green's "Excel 2000 VBA Programmer's
Reference"


I would think this way would work also...

Public Function IsWorkbookOpen(WrkBk As String) As Boolean
Dim WB As Workbook
For Each WB In Workbooks
If StrComp(WB.Name, WrkBk, vbTextCompare) = 0 Then
IsWorkbookOpen = True
Exit For
End If
Next
End Function

Rick

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test to see if a workbook is open Brettjg Excel Discussion (Misc queries) 1 March 5th 07 09:26 AM
Test to see if a workbook is open Alex St-Pierre Excel Programming 3 February 17th 05 05:40 PM
Test if a workbook is open already Kieran1028[_17_] Excel Programming 7 November 22nd 04 08:13 PM
Test that a workbook is open Gef[_2_] Excel Programming 2 April 6th 04 11:17 AM
test for workbook open Rich C Excel Programming 1 March 1st 04 02:52 PM


All times are GMT +1. The time now is 02:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"