Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

There can be only 1

Status
Not open for further replies.

Vamphyri

Technical User
Mar 18, 2005
60
US
I have written the following code to close all workbooks that are not named in the code. However, the code tries to close the specified workbook as well. D'Oh!

I think I'm on the right track, but could use a helping hand.

Code:
Sub CloseAllOthers()
Dim wb As Workbook
Dim wbName As String
For Each wb In Application.Workbooks
    If wbName = "2008 Census (HB 2002) Conversion" Then
    Exit Sub
    Else
    Workbooks.Close
    End If
Next
End Sub

Thanks


In the immortal words of Socrates, who said:
"I drank what?
 
\\\Hi,
Code:
Sub CloseAllOthers()
Dim wb As Workbook

For Each wb In Application.Workbooks
    If thisworkbook.name <> "2008 Census (HB 2002) Conversion.xls" Then
        wb.Close
    End If
Next
End Sub


Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
Hi Vamphyri,

You've got some problems. First off 'wbName' is defined as a string, but what you're after is a workbook name. Secondly (if the naming issue was resolved), the way your sub is coded, it would abort as soon as it encountered the named workbook. If that was the first workbook all the others would remain open. Alternatively, if the named workbook isn't the first one found, 'Workbooks.Close' tries to close all of them.

Try this instead:
Code:
Sub CloseAllOthers()
Dim wb As Workbook
For Each wb In Application.Workbooks
    If wb.Name <> "2008 Census (HB 2002) Conversion.xls" Then wb.Close
Next
End Sub
Note that the name test is case-sensitive and you need to include the extension.

Cheers

[MS MVP - Word]
 
If you intend to close all workbooks except the one the code runs, you can:
Code:
For Each wb In Application.Workbooks
    If Not wb is ThisWorkbook Then wb.Close
Next
It will work for any name of the workbook.


combo
 
A note of caution:

As coded, these macros will also close your Personal.xls workbook (if you have one). That might have undesirable consequences.

Cheers

[MS MVP - Word]
 



Good point macropod...
Code:
Sub CloseAllOthers()
Dim wb As Workbook
For Each wb In Application.Workbooks
    Select Case  wb.Name
       case "2008 Census (HB 2002) Conversion.xls", "Personal.xls"
       case else
          wb.delete
    end select
Next
End Sub


Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
Skip

wb.delete?
should this not be
wb.close?

Never knock on Death's door: ring the bell and run away! Death really hates that!
 



close.

I'd better take a breath.

Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 



"Step away from the keyboard, and put down your mouse!"

Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
Worked like a charm! I would give out stars to all, but am not sure if that is "Kosher" or not. If so, let me know and I'll give them away like candy.

Thanks to all


In the immortal words of Socrates, who said:
"I drank what?
 




If you award stars, it ought to be for [blue]valuable[/blue] posts.

Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top