Save and Close Workbook Macro

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,196
Office Version
  1. 2010
Platform
  1. Windows
Can a macro save and close a workbook? What would a simple one be, and can this be added to existing code? Or run off a timer? Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If the code resides in the workbook you want to save and close, this line can be added to the code at the point where you want to save and close:
Code:
ThisWorkbook.Close SaveChanges:=True
If the code is elsewhere:
Code:
Workbooks("myWorkbook.xls").Close SaveChanges:=True
Substitute for "myWorkbook.xls" the name and file extension of the workbook you want to save and close.
 
Upvote 0
These are the last few lines of code, do I insert this in this way? Thanks for the reply.
Code:
    Else    'skip it and indicate as such
        ws.Range("A" & RowIdx).Offset(0, TrackCol - 1) = "Skipped"
    End If
Next RowIdx
'Go back to the first sheet
ws.Select
Set ws = Nothing
Application.ScreenUpdating = True
    End Sub
Change to
Code:
    End If
Next RowIdx
'Go back to the first sheet
ws.Select
Set ws = Nothing
Application.ScreenUpdating = True
ThisWorkbook.Close SaveChanges:=True
    End Sub
 
Upvote 0
This are different ways and possibilities....one of which.:)

Code:
[/FONT]
[FONT=Courier New]Sub save_and_close()
    ThisWorkbook.Close True
End Sub
 
Upvote 0
The code you have posted will save and close the workbook that code is in.
 
Upvote 0
I got an Expected End Sub error. I used this code at the end.

Code:
ws.Select
Set ws = Nothing
Application.ScreenUpdating = True
Sub save_and_close()
    ThisWorkbook.Close True
End Sub

This part was highlighted, Application.ScreenUpdating = True

Do I need to change somthing? Thanks
 
Upvote 0
Stick with what you posted in Post #3. You don't want or need a separate subroutine.
 
Upvote 0
JoeMo, That works great, Thanks a lot. have a good night. Rocky
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,090
Members
448,944
Latest member
sharmarick

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