+ Reply to Thread
Results 1 to 5 of 5

Macro to close open files without saving it

Hybrid View

Roma1r Macro to close open files... 01-08-2019, 09:11 AM
BellyGas Re: Macro to close open files... 01-08-2019, 09:18 AM
mohan.r1980 Re: Macro to close open files... 01-08-2019, 10:53 AM
mohan.r1980 Re: Macro to close open files... 01-08-2019, 10:24 AM
mohan.r1980 Re: Macro to close open files... 01-09-2019, 02:56 AM
  1. #1
    Registered User
    Join Date
    01-02-2014
    Location
    Cape Town
    MS-Off Ver
    Excel 2007
    Posts
    88

    Macro to close open files without saving it

    Require assistance with setting up a macro.

    The macro the way it’s currently setup reads a filename in a cell reference (data extraction worksheet, cell reference b7) and opens that file, copies data back to the main workbook and then opens the next file (data extraction worksheet, cell reference b8). Before the file copy, it unhides a worksheet (where it’s copying from).

    I need assistance with closing the files opened (in cell reference b7 and b8) within the macro, and no need to save.
    Sub Open_ExistingWorkbook()
    
        filetoopen = ThisWorkbook.Worksheets("data extraction").Range("b7")
        Workbooks.Open Filename:=filetoopen
        Sheets("Payspace").Visible = True
        Sheets("Payspace").Select
        Range("A4").Select
        Range("A4:D117").Select
        Selection.Copy
        Windows("T&O PaySpace upload template_ 2019 - test.xlsm").Activate
        Sheets("Location A").Select
        Range("A7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveWindow.SmallScroll Down:=-21
        Sheets("Data extraction").Select
        Range("A16").Select
             
        filetoopen = ThisWorkbook.Worksheets("data extraction").Range("b8")
        Workbooks.Open Filename:=filetoopen
        Sheets("Payspace").Visible = True
        Sheets("Payspace").Select
        Range("A4").Select
        Range("A4:D117").Select
        Selection.Copy
        Windows("T&O PaySpace upload template_ 2019 - test.xlsm").Activate
        Sheets("Location B").Select
        Range("A7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveWindow.SmallScroll Down:=-21
        Sheets("Data extraction").Select
        Range("A16").Select
           
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Macro to close open files without saving it

    When you open the workbook assign it to a variable:

    
    Dim WorkbookToOpen as Workbook
    
    set WorkbooktoOpen = workbooks.open(Filetoopen)
    Then, to close the workbook you simply:

    
    Application.Displayalerts = false
    WorkbookToOpen.close
    Application.displayalerts = true

  3. #3
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Macro to close open files without saving it

    Hi

    i am trying to make easy your code
    try
    Sub Open_ExistingWorkbook()
    Dim TempWnd As Workbook, fName As String
    Dim r As Range
        
        For Each r In Sheets("Data extraction").Range("B7:B14")
            If r.Address = "$B$7" Then
                filetoopen = ThisWorkbook.Worksheets("data extraction").Range("b7")
                fName = Dir(filetoopen)
                Workbooks.Open Filename:=filetoopen
                Set TempWnd = Workbooks(fName)
                Sheets("Payspace").Visible = True
                Sheets("Payspace").Select
                Range("A4:D117").Copy ThisWorkbook.Sheets("Alrode").Range("A" & Range("A" & Rows.Count).End(xlUp).Row).Offset(5)
                TempWnd.Close False
            End If
            
            If r.Address = "$B$8" Then
                    filetoopen = ThisWorkbook.Worksheets("data extraction").Range("b8")
                    fName = Dir(filetoopen)
                    Workbooks.Open Filename:=filetoopen
                    Set TempWnd = Workbooks(fName)
                    
                    Sheets("Payspace").Visible = True: Sheets("Payspace").Range("A4").Select
                    TempWnd.Close False
                     
            End If
            
        Next r
        
     End Sub
    Regards,
    MohanS


    "Perfection is not attainable, but if we chase perfection we can catch excellence." - Vince Lombardi

    You can simply say thanks by clicking "*Add Reputation" icon

  4. #4
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Macro to close open files without saving it

    try
    Sub Open_ExistingWorkbook()
    Dim TempWnd As Workbook, fName As String
        
        filetoopen = ThisWorkbook.Worksheets("data extraction").Range("b7")
        
        fName = Dir(filetoopen)
        Workbooks.Open Filename:=filetoopen
        Set TempWnd = Workbooks(fName)
        
        Sheets("Payspace").Visible = True
        Sheets("Payspace").Select
        Range("A4").Select
        Range("A4:D117").Select
        Selection.Copy
        Windows("T&O PaySpace upload template_ 2019 - test.xlsm").Activate
        Sheets("Alrode").Select
        Range("A7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveWindow.SmallScroll Down:=-21
        Sheets("Data extraction").Select
        Range("A16").Select
        
        TempWnd.Close False
        
        filetoopen = ThisWorkbook.Worksheets("data extraction").Range("b8")
        
        fName = Dir(filetoopen)
        Workbooks.Open Filename:=filetoopen
        Set TempWnd = Workbooks(fName)
        
        Sheets("Payspace").Visible = True
        Sheets("Payspace").Select
        Range("A4").Select
        
        TempWnd.Close False
    End Sub

  5. #5
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Macro to close open files without saving it

    Your welcome and thanks for rep+

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Open and close files using excel macro
    By laxmanann in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-16-2014, 12:06 PM
  2. [SOLVED] macro to open all files in a folder, save them, then close them
    By hopefulhart in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-27-2013, 06:40 PM
  3. [SOLVED] Macro to open and close all files in a folder and sub folders
    By BillDoor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-31-2012, 06:51 AM
  4. Macro to open, copy/paste, and close files with variable name
    By mjr33 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-15-2011, 10:20 PM
  5. Macro to Open, Save and Close Excel Files
    By az! in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2011, 04:19 AM
  6. Macro to Open, Print and Close multiple PDF Files
    By Herve_Rob in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-09-2009, 05:08 AM
  7. Macro to open all files, make changes, save and close
    By certain_death in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2009, 03:42 PM

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