Hi Hans,
The objective is to save close and Quit Excel only if one workbook is opened.
If more than one workbook is opened then Save and Close the active one and exit only.
Regards,
Chuck
Macro needed to close only active Workbook
-
- 3StarLounger
- Posts: 372
- Joined: 17 Dec 2013, 00:07
-
- Administrator
- Posts: 78596
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro needed to close only active Workbook
Where do you want to store this macro? In your personal macro workbook Personal.xlsb, in an Excel add-in or in a specific workbook?
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 372
- Joined: 17 Dec 2013, 00:07
Re: Macro needed to close only active Workbook
In the active workbook.
Chuck
Chuck
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: Macro needed to close only active Workbook
What do you mean by exit only?
One WB; save, close and exit Excel (understood!)
More than one WB; save and close the active one (understood!) - but exit what???
One WB; save, close and exit Excel (understood!)
More than one WB; save and close the active one (understood!) - but exit what???
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- Administrator
- Posts: 78596
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro needed to close only active Workbook
Perhaps this? It also takes Personal.xlsb into account:
Code: Select all
Sub CloseMe()
If Workbooks.Count = 1 Or Workbooks.Count = 2 And _
(LCase(Workbooks(1).Name) = "personal.xlsb" Or _
LCase(Workbooks(2).Name) = "personal.xlsb") Then
ActiveWorkbook.Save
Application.Quit
Else
ActiveWorkbook.Close SaveChanges:=True
End If
End Sub
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 372
- Joined: 17 Dec 2013, 00:07
Re: Macro needed to close only active Workbook
Sorry Hans for not being clear.
I may have multiple workbooks opened while working, I want for one particular workbook when run, if the user select the button to Save & Close then the macro will only save and close this particular workbook and close it but Excel will not close so the other opened workbooks will not be affected.
Now if only this particular workbook is openned then when the user click the button then it will save, Close and quit Excel.
Hope this is clear.
Regards,
Chuck
Below is and example of waht I wrote but crashed my connection to the network.
I may have multiple workbooks opened while working, I want for one particular workbook when run, if the user select the button to Save & Close then the macro will only save and close this particular workbook and close it but Excel will not close so the other opened workbooks will not be affected.
Now if only this particular workbook is openned then when the user click the button then it will save, Close and quit Excel.
Hope this is clear.
Regards,
Chuck
Below is and example of waht I wrote but crashed my connection to the network.
Code: Select all
Public Sub SaveActiveWbOnly()
Dim AwB As String
AwB = ActiveWorkbook.Name
Dim Wb As Workbook
For Each Wb In Workbooks
If Workbooks.Count < 2 Then
Application.DisplayAlerts = False
Wb.Save
Wb.Close
Application.StatusBar = "Active.Wb Saved."
Application.DisplayAlerts = True
GoTo QuitProcess
End If
If ActiveWorkbook.Name = AwB Then
Wb.Save
Wb.Close
Else
Wb.Save
End If
Application.StatusBar = Wb.Name & " Workbook has been Saved."
Next Wb
Exit Sub
QuitProcess:
Application.StatusBar = ""
Application.Quit
End Sub
-
- Administrator
- Posts: 78596
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro needed to close only active Workbook
Have you tried the macro that I posted?
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 372
- Joined: 17 Dec 2013, 00:07
Re: Macro needed to close only active Workbook
Chuck
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 78596
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro needed to close only active Workbook
Sorry, try this version:
Code: Select all
Sub CloseMe()
If Workbooks.Count = 1 Then
ActiveWorkbook.Save
Application.Quit
ElseIf Workbooks.Count = 2 And _
(LCase(Workbooks(1).Name) = "personal.xlsb" Or _
LCase(Workbooks(2).Name) = "personal.xlsb") Then
ActiveWorkbook.Save
Application.Quit
Else
ActiveWorkbook.Close SaveChanges:=True
End If
End Sub
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 372
- Joined: 17 Dec 2013, 00:07
Re: Macro needed to close only active Workbook
It worked!
Thanks!
Chuck
P.S.: What does mean a variable that is declared outside of the Sub.
e.g.:
Thanks!
Chuck
P.S.: What does mean a variable that is declared outside of the Sub.
e.g.:
Code: Select all
Dim Ps As Integer
Subt Whatever()
End Sub
-
- Administrator
- Posts: 78596
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro needed to close only active Workbook
It means that the variable is available to all procedures (Subs) and functions within the same module. If you use the keyword Public, as in
Public Ps As Integer
the variable will be available to all procedures and subs in the same workbook. See A matter of scope: visibility of code elements.
Public Ps As Integer
the variable will be available to all procedures and subs in the same workbook. See A matter of scope: visibility of code elements.
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 372
- Joined: 17 Dec 2013, 00:07