Close all the other workbooks except current workbook based on a vbYes and vbNo answer

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Code:
Sub isAnyWorkbookOpen()        
    Dim wb As Workbook
    Dim wbs As Workbooks
    Dim msg As String
    Dim Result
    
    msg = "The following workbook(s) must be closed before continuing:" & Chr(10) & "Do you want to close?" & Chr(10) & Chr(10)
    
    Set wbs = Application.Workbooks
    
    For Each wb In wbs
        If wb.Name <> ThisWorkbook.Name Then msg = msg & wb.Name & Chr(10)
    Next wb
    
    If Len(msg) > 0 Then
        Result = MsgBox(msg, 52, "Workbooks Open")
    Else
         [COLOR=#008000]'Close MessageBox[/COLOR]
    End If
    If Result = vbYes Then
        wb.Close
    Else
        
    End If
End Sub
The problem with code above is that I get a "Runtime error 91: Object variable or with block variable not set" and when I click debug it highlights "wb.Close" I just want to close all the workbooks except the current one. Also if the user clicks "No" then I need the messagebox to close. I already tried
Code:
Cancel = True
but I get a "Compile error: Variable not defined" with the word Cancel highlighted in blue. However, on this issue I shouldn't have to do anything because when I click the No button it automatically closes the msgbox anyways. Am I wrong on that observation?

Thank you
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Assuming the code is in the workbook that is the only workbook you want open (except a hidden Personal workbook if you have one), you don't really need a message box.
Code:
Sub CloseAllButThisOne()
Dim wb As Workbook
For Each wb In Application.Workbooks
    If wb.Name <> "PERSONAL.XLSB" And wb.Name <> ThisWorkbook.Name Then
        wb.Close
    End If
Next wb
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,317
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top