+ Reply to Thread
Results 1 to 3 of 3

Close open files following "With Workbook.Open" cycle

Hybrid View

  1. #1
    Registered User
    Join Date
    04-07-2014
    Location
    n/a
    MS-Off Ver
    Excel 2010
    Posts
    13

    Close open files following "With Workbook.Open" cycle

    Hi,

    I created a macro that goes trhough all files in a specified folders, "extracts" 3 sheets and then copies these 3 sheets into a new folder, with a slightly different name.

    I am puzzled on how to close the "original" file that I open, the ones that I cycle through; the final result is that I have all the "new" files saved and closed and in the correct folder, but I still have all the "source" files open when the process is finished. Minor nuisance, but still I would like to solve it.

    Here below the code:


    FolderName = "C:\Users\Research05\Desktop\Test"
    DestFoldName = "C:\Users\Research05\Desktop\Data\Export"
    If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
    FName = Dir(FolderName & "*.xlsm")

    'loop through the files
    Do While Len(FName)

    With Workbooks.Open(FolderName & FName, UpdateLinks:=0)

    Dim AWBn As String
    Dim DestName As String
    AWBn = ActiveWorkbook.Name
    DestName = Left(AWBn, InStr(AWBn, "Master.xlsm"))

    Application.ScreenUpdating = False

    Worksheets(Array("Sheets1", "Sheets2", "Sheets3")).Copy

    Set Wb = ActiveWorkbook

    Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
    Sheets("Sheet1").Activate
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("U64").Select
    Sheets("Sheet1").Select

    Wb.SaveAs DestFoldName & "\" & DestName & " Data"
    Wb.Close

    End With

    ' go to the next file in the folder
    FName = Dir

    Loop

    End Sub

  2. #2
    Forum Contributor
    Join Date
    01-20-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    186

    Re: Close open files following "With Workbook.Open" cycle

    Hi Accicarelli,

    I would suggest this:

    Bind the sourcesheet just after the opening, and close it just after saving the target.
    With Workbooks.Open(FolderName & FName, UpdateLinks:=0)
    Set WbSource = ActiveWorkbook
    ...
    [other code]
    ...
    WbSource.Close
    End With
    ...

  3. #3
    Registered User
    Join Date
    04-07-2014
    Location
    n/a
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Close open files following "With Workbook.Open" cycle

    thank you, it's exactly the solution I was looking for!

+ 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. [SOLVED] Must open and close UserForm twice for the other codes in the "Open..." Macro to execute
    By tnuis in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-10-2013, 09:28 AM
  2. Replies: 1
    Last Post: 05-23-2013, 02:04 PM
  3. Open files from FTP server (works with "servername" but doesn't work with "ip address")
    By adammsu1983 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-24-2012, 04:30 PM
  4. [SOLVED] Loop through ".DAT" files, open run code, close next
    By Les Stout in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2006, 05:20 AM
  5. Replies: 0
    Last Post: 01-04-2006, 06:55 PM

Tags for this Thread

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