+ Reply to Thread
Results 1 to 5 of 5

Open/RunMacro/Save&Close Macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Open/RunMacro/Save&Close Macro

    I am using the following code to open a group of workbooks and run a macro inside and then save and close the workbook:
    Sub UpdateAll()
        Dim folderPath As String
        Dim filename As String
        Dim wb As Workbook
      
        folderPath = "K:\Location\"
        
        If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
        
        filename = Dir(folderPath & "*.xlsm")
        Do While filename <> ""
          Application.ScreenUpdating = False
            Set wb = Workbooks.Open(folderPath & filename)
             
    Application.Run "'" & filename & "'!UpdateData"
            
       wb.Close SaveChanges:=True
        filename = Dir
     Loop
      Application.ScreenUpdating = True
    End Sub
    I added the below to the end of each individual macro within each workbook and now UpdateAll() won't go through each workbook in the folder and run the workbooks, it stops after opening one:

        
    Dim Output As Workbook
        Dim Current As String
        Dim FileName As String
    
        Set Output = ThisWorkbook
        Current = ThisWorkbook.FullName
    
        Application.DisplayAlerts = False
    
        Dim SH As Worksheet
        For Each SH In Output.Worksheets
    
            SH.UsedRange.Copy
            SH.UsedRange.PasteSpecial xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=True, Transpose:=False
    
        Next
    
        FileName = ThisWorkbook.Path & "S:\Location\" & "165.xlsx"
          Output.SaveAs FileName:="S:\Location\165.xlsx", _
                    FileFormat:=xlOpenXMLWorkbook
        Workbooks.Open Current
        Output.Close
        Application.DisplayAlerts = True
    
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Open/RunMacro/Save&Close Macro

    Hi,

    The line in red below is probably the problem. The macro closes the file that is running the code. This terminates the Macro 'daisy chain' and causes all Macro activity to stop.

    The solution is to have the father routine close the son file, like you do in the top macro.

    Dim Output As Workbook
        Dim Current As String
        Dim FileName As String
    
        Set Output = ThisWorkbook
        Current = ThisWorkbook.FullName
    
        Application.DisplayAlerts = False
    
        Dim SH As Worksheet
        For Each SH In Output.Worksheets
    
            SH.UsedRange.Copy
            SH.UsedRange.PasteSpecial xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=True, Transpose:=False
    
        Next
    
        FileName = ThisWorkbook.Path & "S:\Location\" & "165.xlsx"
          Output.SaveAs FileName:="S:\Location\165.xlsx", _
                    FileFormat:=xlOpenXMLWorkbook
        Workbooks.Open Current
        Output.Close
        Application.DisplayAlerts = True
    End Sub
    Lewis

  3. #3
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Open/RunMacro/Save&Close Macro

    Thanks Lewis, I commented that out and it turns over to open the file well.
    If I comment out:
    Workbooks.Open Current
    That will close the current book and still go onto the next one in the folder, correct?

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Open/RunMacro/Save&Close Macro

    If I comment out 'Workbooks.Open Current' that will close the current book and still go onto the next one in the folder, correct?
    Unfortunately, No. Workbooks.Open does not close files. All the previous workbooks you had open will still be open. The code has to be structured in such a way that closing a file does not break the daisy chain.

    See the following thread which discusses your problem: http://www.excelforum.com/excel-prog...le-macros.html

    There are download file associated with post #16 that you might want to try. Norie has a solution (have all the macros in one master workbook) that works in post #17, but Norie's solution is not always possible.

    I hope this helps. It is a complicated situation. Please ask if you are still having questions and/or problems.

    Lewis

  5. #5
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Open/RunMacro/Save&Close Macro

    Thanks Lewis!

+ 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] Macro to open/close/save workbook
    By JonathanB2 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-14-2014, 10:51 AM
  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. Need macro to open, refresh, save, close 50 workbooks in a folder.
    By paulcg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2012, 09:23 AM
  4. Macro to close all open workbooks save two not working
    By drajanm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-18-2011, 05:58 AM
  5. Macro to Open a file, transfer data, save and close
    By tforbes75 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2011, 11:59 AM
  6. 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
  7. Open WB / Run Macro / Save changes /Close WB
    By nachousa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2010, 01:08 PM
  8. 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

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