PC Review


Reply
Thread Tools Rate Thread

Closing workbooks programatically

 
 
teepee
Guest
Posts: n/a
 
      20th Feb 2008
My problem is I want to use VBA to close 2 workbooks if they're open and do
nothing if they're not open.

Put it another way, if I put


Workbooks("book1.csv").Close SaveChanges:=False
Workbooks("book2.csv").Close SaveChanges:=False

and then book 1 and book 2 aren't already open, it creates a runtime error
which I don't want.

I thought this might work:

Dim wb As Workbook
If wb.Name = "book1.csv" Then
Workbooks("book1").Close SaveChanges:=False
'End If
'If wb.Name = "book2" Then
Workbooks("book2").Close SaveChanges:=False
'End If

But it doesn't. Anyone got any ideas?


 
Reply With Quote
 
 
 
 
teepee
Guest
Posts: n/a
 
      20th Feb 2008

"teepee" <(E-Mail Removed)> wrote in message
news:47bc9a2d$(E-Mail Removed)...
> My problem is I want to use VBA to close 2 workbooks if they're open and
> do nothing if they're not open.


btw I found another way of doing it so that I didn't need to do this but I'd
still be interested to know if anyone knows how it might be done.


 
Reply With Quote
 
PCLIVE
Guest
Posts: n/a
 
      20th Feb 2008
Here is a short example to determine if a workbook is open. You should be
able to apply this to what you're doing.

Sub test()
On Error GoTo Skip
If Len(Application.Workbooks(Range("A1").Value).Name) > 0 _
Then
MsgBox ("workbook is open")
Exit Sub
Else
End If

Skip:
MsgBox ("workbook is not open")
End Sub


HTH,
Paul


--

"teepee" <(E-Mail Removed)> wrote in message
news:47bc9fcf$(E-Mail Removed)...
>
> "teepee" <(E-Mail Removed)> wrote in message
> news:47bc9a2d$(E-Mail Removed)...
>> My problem is I want to use VBA to close 2 workbooks if they're open and
>> do nothing if they're not open.

>
> btw I found another way of doing it so that I didn't need to do this but
> I'd still be interested to know if anyone knows how it might be done.
>



 
Reply With Quote
 
teepee
Guest
Posts: n/a
 
      20th Feb 2008

"PCLIVE" <pclive(RemoveThis)@cox.net> wrote in message
news:(E-Mail Removed)...
> Here is a short example to determine if a workbook is open. You should be
> able to apply this to what you're doing.


Many thanks


 
Reply With Quote
 
Tyro
Guest
Posts: n/a
 
      20th Feb 2008
This might do what you want

Sub CloseThem()
Dim wb As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each wb In Application.Workbooks
If wb.Name = "book1.csv" or wb.Name = "book2.csv" Then
' wb.Save
wb.Close
End If
Next wb
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

Tyro


"teepee" <(E-Mail Removed)> wrote in message
news:47bc9a2d$(E-Mail Removed)...
> My problem is I want to use VBA to close 2 workbooks if they're open and
> do nothing if they're not open.
>
> Put it another way, if I put
>
>
> Workbooks("book1.csv").Close SaveChanges:=False
> Workbooks("book2.csv").Close SaveChanges:=False
>
> and then book 1 and book 2 aren't already open, it creates a runtime error
> which I don't want.
>
> I thought this might work:
>
> Dim wb As Workbook
> If wb.Name = "book1.csv" Then
> Workbooks("book1").Close SaveChanges:=False
> 'End If
> 'If wb.Name = "book2" Then
> Workbooks("book2").Close SaveChanges:=False
> 'End If
>
> But it doesn't. Anyone got any ideas?
>
>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      20th Feb 2008
Is there a reason you can't ignore the error:

On error resume next
Workbooks("book1.csv").Close SaveChanges:=False
Workbooks("book2.csv").Close SaveChanges:=False
on error goto 0

Since you're discarding any changes, it doesn't seem like this would cause a
problem.



teepee wrote:
>
> My problem is I want to use VBA to close 2 workbooks if they're open and do
> nothing if they're not open.
>
> Put it another way, if I put
>
> Workbooks("book1.csv").Close SaveChanges:=False
> Workbooks("book2.csv").Close SaveChanges:=False
>
> and then book 1 and book 2 aren't already open, it creates a runtime error
> which I don't want.
>
> I thought this might work:
>
> Dim wb As Workbook
> If wb.Name = "book1.csv" Then
> Workbooks("book1").Close SaveChanges:=False
> 'End If
> 'If wb.Name = "book2" Then
> Workbooks("book2").Close SaveChanges:=False
> 'End If
>
> But it doesn't. Anyone got any ideas?


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Closing workbooks w/o closing Excel Barb in MD Microsoft Excel Misc 4 15th Feb 2010 06:44 PM
Multiple workbooks open -- closing workbooks =?Utf-8?B?SmFuZXQgUGFuaWdoZXR0aQ==?= Microsoft Excel Discussion 3 13th Nov 2007 05:43 PM
Closing Access 2003 Programatically =?Utf-8?B?Qnl6YW50aW5l?= Microsoft Access 2 12th Jan 2005 02:22 PM
Closing excel application without closing all workbooks at the sa. =?Utf-8?B?S2l3aSBSb3hpZQ==?= Microsoft Excel Setup 2 30th Oct 2004 02:18 AM
Closing a modal dialog programatically Randy A. Ynchausti Microsoft C# .NET 1 8th Sep 2004 10:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:51 AM.