Solved

Close a file vba

Posted on 2014-02-04
4
362 Views
Last Modified: 2014-02-05
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
Comment
Question by:leezac
  • 2
4 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 39834183
Have you tried something like this:

    ActiveWorkbook.Close
    application.Quit

Open in new window

0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39834212
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
 

Author Comment

by:leezac
ID: 39835931
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 39836239
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
modify formula same cell multiple times 15 29
Move data from one sheet to another 11 32
Excel Cell Total 3 20
formatting - number format 2 12
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question