Save and Close Workbook Macro

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,191
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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,585
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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.
 

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,191
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
 

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
This are different ways and possibilities....one of which.:)

Code:
[/FONT]
[FONT=Courier New]Sub save_and_close()
    ThisWorkbook.Close True
End Sub
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,585
Office Version
  1. 365
  2. 2010
Platform
  1. Windows

ADVERTISEMENT

The code you have posted will save and close the workbook that code is in.
 

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,191
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
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,585
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Stick with what you posted in Post #3. You don't want or need a separate subroutine.
 

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,191
JoeMo, That works great, Thanks a lot. have a good night. Rocky
 

Forum statistics

Threads
1,144,370
Messages
5,723,960
Members
422,529
Latest member
mbilal429

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
Top