Solved

Close a file vba

Posted on 2014-02-04
4
359 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
Comment Utility
Have you tried something like this:

    ActiveWorkbook.Close
    application.Quit

Open in new window

0
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now