Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How to find if a workbook is open

I want to open a workbook, say "A", to perform some
calculation.However, if the workbook is already open then
I want to activate it and do the calculations. I am a
little bit confused about the code I need to decide
whether it is open or need opening. Can anyone help?
Thanks in advance.

Regards
Amir
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to find if a workbook is open

Hi Amir,

This should do it

Function IsWBOpen(Name As String)
Dim oWB As Workbook

On Error Resume Next
Set oWB = Workbooks(Name)
On Error GoTo 0
IsWBOpen = Not (oWB Is Nothing)

End Function


Test with

If IsWBOpen("Stats Manager.xls") Then
Workbooks(Stats Manager.xls").Activate
Else
Workbooks.Open Filename:="Stats Manager.xls"
End If
....


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Amir" wrote in message
...
I want to open a workbook, say "A", to perform some
calculation.However, if the workbook is already open then
I want to activate it and do the calculations. I am a
little bit confused about the code I need to decide
whether it is open or need opening. Can anyone help?
Thanks in advance.

Regards
Amir



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to find if a workbook is open

If you want to avoid use an On Error block, try following function :

Function IsWBOpen(strName As String) as Boolean

Dim w as Workbook,
Dim bResult as Boolean

bResult = False

For each w in Workbooks
if w.name = strName then
bResult = True
exit for
end if
Next

IsWBOpen = bResult

End Functio

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How to find if a workbook is open

Bob and Tolgag,

Thank you ever so much.It works and will save me a lot of
troubleshooting. This group is great. Thank you.

Regards

Amir

-----Original Message-----
Hi Amir,

This should do it

Function IsWBOpen(Name As String)
Dim oWB As Workbook

On Error Resume Next
Set oWB = Workbooks(Name)
On Error GoTo 0
IsWBOpen = Not (oWB Is Nothing)

End Function


Test with

If IsWBOpen("Stats Manager.xls") Then
Workbooks(Stats Manager.xls").Activate
Else
Workbooks.Open Filename:="Stats Manager.xls"
End If
....


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Amir" wrote in

message
...
I want to open a workbook, say "A", to perform some
calculation.However, if the workbook is already open

then
I want to activate it and do the calculations. I am a
little bit confused about the code I need to decide
whether it is open or need opening. Can anyone help?
Thanks in advance.

Regards
Amir



.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to find if a workbook is open

Why would one want to avoid an on Error block by replacing it with a slower
approach like looping through all the workbooks? Is there a technical
reason or just personal preference?

--
Regards,
Tom Ogilvy

tolgag wrote in message
...
If you want to avoid use an On Error block, try following function :

Function IsWBOpen(strName As String) as Boolean

Dim w as Workbook,
Dim bResult as Boolean

bResult = False

For each w in Workbooks
if w.name = strName then
bResult = True
exit for
end if
Next

IsWBOpen = bResult

End Function


---
Message posted from http://www.ExcelForum.com/



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
How to: Open closed workbook/Search data tables/Return data to open workbook Hugh Adams Excel Discussion (Misc queries) 0 August 18th 10 02:04 PM
Opening Excel, Book1 opens, remains open with other workbook open DanieB Excel Discussion (Misc queries) 0 September 3rd 09 08:23 AM
how do i open a data workbook when i open a timesheet workbook [email protected] uk Excel Discussion (Misc queries) 2 January 4th 09 04:50 PM
How to find file-path of open workbook? Larryq22 Excel Discussion (Misc queries) 7 January 24th 08 03:38 AM
excel 2003 saved file will not open without a blank workbook open Bob Excel Discussion (Misc queries) 4 November 11th 06 04:24 PM


All times are GMT +1. The time now is 02:09 PM.

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

About Us

"It's about Microsoft Excel"