+ Reply to Thread
Results 1 to 2 of 2

Close only active file and not all excel files

Hybrid View

lfmemp Close only active file and... 11-29-2012, 03:57 PM
DobbsHead Re: Close only active file... 11-29-2012, 04:11 PM
  1. #1
    Registered User
    Join Date
    06-14-2012
    Location
    Brussels
    MS-Off Ver
    Excel 2010
    Posts
    16

    Close only active file and not all excel files

    Hi there,

    I am using the below code for a file I need to save and close and password protect. The code works fine, but the only thing is that when is closes, it does shut down all my excel files that I have open. What I want it to do is to save and close only that active file where the macro is in and not all my excel files I ahve open at that moment. Can please somebody help me?

    Thanks




    Public CloseMode As Boolean
    Sub Save_Close()
    Dim Passwrd As String
    Dim xx As Variant
    Dim sFileFormat
    Passwrd = "PASSWORD"
    xx = MsgBox("Do you want to close the file?", vbYesNo)
    If xx <> vbYes Then
    Cancel = True
    Else
    'fileformats
    ' xls = -4143 < 2007
    ' xlsm = 52 2007 >
    With Application
    .DisplayAlerts = False
    sFileFormat = IIf(.Version < 12, -4143, 52)
    End With
    With ThisWorkbook
    .SaveAs Filename:=.FullName, _
    FileFormat:=sFileFormat, _
    Password:=Passwrd, _
    WriteResPassword:="", _
    ReadOnlyRecommended:=False, _
    CreateBackup:=False
    .Saved = True
    End With
    'allow workbook to close
    CloseMode = False
    With Application
    .DisplayAlerts = True
    .Quit
    End With
    End If
    End Sub

  2. #2
    Registered User
    Join Date
    09-05-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Close only active file and not all excel files

    First off, please wrap your code in code tags. Also please, for the love of god, format your code better. Seriously.

    Try the code below.

    Public CloseMode As Boolean
    Sub Save_Close()
    Dim Passwrd As String
    Dim xx As Variant
    Dim sFileFormat
    
    Passwrd = "PASSWORD"
    xx = MsgBox("Do you want to close the file?", vbYesNo)
    
    If xx <> vbYes Then
    Cancel = True
    Else 'Why did you leave these comments in?
    'fileformats
    ' xls = -4143 < 2007
    ' xlsm = 52 2007 >
    With Application
    .DisplayAlerts = False
    sFileFormat = IIf(.Version < 12, -4143, 52)
    End With
    With ThisWorkbook
    .SaveAs Filename:=.FullName, _
    FileFormat:=sFileFormat, _
    Password:=Passwrd, _
    WriteResPassword:="", _
    ReadOnlyRecommended:=False, _
    CreateBackup:=False
    .Saved = True
    .Close '<--- This will close the workbook, not the application
    End With
    'This is where you told the application (not the workbook) to close
    'CloseMode = False
    'With Application '<-- See, the word "application" is a clue as to what you are changing
    '.DisplayAlerts = True
    '.Quit
    'End With
    End If End Sub
    Last edited by DobbsHead; 11-29-2012 at 04:16 PM. Reason: Formatting came out weird the first time

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1