Macro close don't save

dsrt16

Board Regular
Joined
Jun 18, 2005
Messages
208
I have a macro that automatically closes out of all open workbooks; however, I want it to close without saving the read only documents. Right now it closes out of everything, but a pop up appears asking if the user wants to save the read only worksheet.

I don't want that pop-up to appear. I want it to just exit out and save all spreadsheets except the read only ones WITHOUT asking.

Here is the current code:

Code:
Sub CloseOpenWorkbooks()
    Dim Wkb As Workbook
     
    With Application
        .ScreenUpdating = False
         
        For Each Wkb In Workbooks
            With Wkb
                 ' If the book is read-only
                 ' don't save but close
                If Not Wkb.ReadOnly Then
                    .Save
                End If
                 ' save this workbook, but don't close it
                 ' so the macro still runs
                If .Name <> ThisWorkbook.Name Then
                    .Close
                End If
            End With
        Next Wkb
        .ScreenUpdating = True
        .Quit
    End With
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Not Tested: In a copy of your wb, try:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> CloseOpenWorkbooks()<br>    <SPAN style="color:#00007F">Dim</SPAN> Wkb <SPAN style="color:#00007F">As</SPAN> Workbook<br>     <br>    <SPAN style="color:#00007F">With</SPAN> Application<br>        .ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>         <br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Wkb <SPAN style="color:#00007F">In</SPAN> Workbooks<br>            <SPAN style="color:#00007F">With</SPAN> Wkb<br>                 <SPAN style="color:#007F00">' If the book is read-only</SPAN><br>                 <SPAN style="color:#007F00">' don't save but close</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Wkb.ReadOnly _<br>                And <SPAN style="color:#00007F">Not</SPAN> .Name = ThisWorkbook.Name <SPAN style="color:#00007F">Then</SPAN><br>                    .Close <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">ElseIf</SPAN> <SPAN style="color:#00007F">Not</SPAN> Wkb.Name = ThisWorkbook.Name <SPAN style="color:#00007F">Then</SPAN><br>                    .Close <SPAN style="color:#00007F">False</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> Wkb<br>        <br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> ThisWorkbook.Saved <SPAN style="color:#00007F">Then</SPAN> ThisWorkbook.Save<br>        <br>        .ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>        .Quit<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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