• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

Close a file vba

Using the code below to open a file.  How would I close the file?


    DIV = Application.GetOpenFilename(FileFilter:="xls*.Files (*.xls*), *.xls*", Title:="Please select a file")
   

      'browse boxes may be on the main parm sheet i.e. Sheets("Main").VAR_DIV = DIV
 '    Application.EnableEvents = False
'Application.DisplayAlerts = False
Sheets("Variances").VAR_DIV = DIV


     Workbooks.Open (DIV)
0
leezac
Asked:
leezac
  • 2
1 Solution
 
rspahitzCommented:
Have you tried something like this:

    ActiveWorkbook.Close
    application.Quit

Open in new window

0
 
byundtCommented:
If you have been selecting cells or worksheets, the active workbook may not be the one that you want to close. It is better practice to specify the workbook by name, or by using a variable pointing to it. Both alternatives are shown in the snippet below.
Dim DIV As String, flName As String
Dim wb As Workbook
DIV = Application.GetOpenFilename(FileFilter:="xls*.Files (*.xls*), *.xls*", Title:="Please select a file")
flName = Mid(DIV, InStrRev(Application.PathSeparator & DIV, Application.PathSeparator))
      'browse boxes may be on the main parm sheet i.e. Sheets("Main").VAR_DIV = DIV
 '    Application.EnableEvents = False
'Application.DisplayAlerts = False
Sheets("Variances").VAR_DIV = DIV

Set wb = Workbooks.Open(DIV)

    'Close the workbook without displaying a warning about unsaved file. Two alternatives are shown.
Workbooks(flName).Close SaveChanges:=False      'Close the file using its name
wb.Close SaveChanges:=False      'Close the file using a workbook variable

Open in new window

0
 
leezacAuthor Commented:
Brad - this is in relation to last post you helped with

This is what I am using to close.  the close line did not work until I added the Set line.  Is that correct though?
__________________________________________________________
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row
 
    Range("G4:G7" & Lastrow).Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Cap Gain Div Cust Variance Template.xlsm").Activate
     Range("L3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   'Close the workbook without displaying a warning about unsaved file. Two alternatives are shown.

Set wb = Workbooks.Open(DIV)
wb.Close SaveChanges:=False      'Close the file using a workbook
End Sub
0
 
rspahitzCommented:
In VBA, "Set" is required whenever assigning objects (such as workbooks and Sheets) to variables.
It's not needed for lines like this:
Application.CutCopyMode = False
because the property is not an object.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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