Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Macro to Close Excel

Experts,
I would like to create a command button that runs a macro that when selected closes Excel, asking to save changes, if changes have been made.
The name of my Workbook is Monthly
0
Frank Freese
Asked:
Frank Freese
  • 4
  • 4
  • 2
1 Solution
 
Ardhendu SarangiSr. Project ManagerCommented:
Here's the code -
Close-Me.xls
0
 
Ardhendu SarangiSr. Project ManagerCommented:
The code used is


Sub closeme()
ThisWorkbook.Close
End Sub
0
 
jppintoCommented:
Is this what you want?
Dim strMsg As String
Dim intQ As String

strMsg = "Do you want to save changes?"
intQ = MsgBox(strMsg, vbQuestion + vbYesNo, "Save changes?")

If intQ = vbYes Then
    ActiveWorkbook.SaveAs Filename:=Application.GetSaveAsFilename, FileFormat:=52
End If
ActiveWorkbook.Close

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Frank FreeseAuthor Commented:
jp: close...real close...I like MsgBox...If the user wants to save changes can I execute a ActiveWorkbook.Save, ActiveWorkbook.Close, and ActiveWorkbook.Quit (not Activeworkbook.SaveAs)
0
 
jppintoCommented:
You can make a .SaveAs or .Save

I don't understand your question... sorry.
0
 
Frank FreeseAuthor Commented:
Here's my code. My objective would be to save the file, close the program and exit excel:

Sub CloseExcel()

Dim strMsg As String
Dim intQ As String

strMsg = "Do you want to save changes?"
intQ = MsgBox(strMsg, vbQuestion + vbYesNo, "Save changes?")

If intQ = vbYes Then
ActiveWorkbook.Save Filename:=Application.GetSaveAsFilename, FileFormat:=52

End If
ActiveWorkbook.Close
ActiveWorkbook.Quit
End Sub
0
 
jppintoCommented:
Like this should do it:

Sub CloseExcel()

Dim strMsg As String
Dim intQ As String

strMsg = "Do you want to save changes?"
intQ = MsgBox(strMsg, vbQuestion + vbYesNo, "Save changes?")

If intQ = vbYes Then
ActiveWorkbook.Save
End If
Application.Quit
End Sub
0
 
Frank FreeseAuthor Commented:
Perfect! Thank you - I was close (almost)
0
 
Frank FreeseAuthor Commented:
great job - thank you
0
 
jppintoCommented:
You're welcome and thanks for the grade.

jppinto
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now