+ Reply to Thread
Results 1 to 8 of 8

Thread: macro to open and close Excel workbooks

  1. #1
    Registered User
    Join Date
    12-09-2010
    Location
    Grand Cayman
    MS-Off Ver
    Excel 2007
    Posts
    3

    macro to open and close Excel workbooks

    I have created a system to manage mortgage accounting. Each mortgage has it's own spreadsheet. File names: mortgage.xlsx, mortgage1.xlsx, mortgage2.xlsx up to mortgage49.xlsx.

    I have created a master control spreadsheet that pulls information from all the accounts (spreadsheets).
    Some of the spreadsheets may not exist, so to prevent a Macro error the file is only opened if it exists. Thus:

    If Dir("C:\Users\Owner\Documents\MortAcc\mortgage.xlsx") <> "" Then
    Workbooks.Open ("C:\Users\Owner\Documents\MortAcc\mortgage.xlsx")
    
    End If
    
    If Dir("C:\Users\Owner\Documents\MortAcc\mortgage1.xlsx") <> "" Then
    Workbooks.Open ("C:\Users\Owner\Documents\MortAcc\mortgage1.xlsx")
       
    End If
    This part works fine. Having opened them I then need to close them in the same Macro.

    However the following does not work and results in a Runtime error 9. Subscript out of range.
    This trips up at the first statement below, which IS an existing file:

    If Dir("C:\Users\Owner\Documents\MortAcc\mortgage.xlsx") <> "" Then
    Workbooks("C:\Users\Owner\Documents\MortAcc\mortgage.xlsx").Close SaveChanges:=False
    
    End If
    
    If Dir("C:\Users\Owner\Documents\MortAcc\mortgage1.xlsx") <> "" Then
    Workbooks("C:\Users\Owner\Documents\MortAcc\mortgage1.xlsx").Close SaveChanges:=False
       
    End If
    Last edited by Paul; 12-09-2010 at 05:44 PM. Reason: Added code tags for new user. Please do so yourself in the future.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: macro to open and close Excel workbooks

    We could parse the 1-49, too with another loop, but just in case you want to use any random text strings for your workbook names, you can put them all into an array of string values like so, then just run them all:

    Option Explicit
    
    Sub OpenClose()
    Dim fileARR As Variant
    Dim fName As Long
    
    fileARR = Array("mortgage", "mortgage1", "mortgage2", "mortgage3", "mortgage4", "mortgage5", _
            "mortgage6", "mortgage7", "mortgage8", "mortgage9", "mortgage10", "mortgage11", _
            "mortgage12", "mortgage13", "mortgage14", "mortgage15", "mortgage16,", "mortgage17", _
            "mortgage18", "mortgage19", "mortgage20", "mortgage21", "mortgage22,", "mortgage23", _
            "mortgage24", "mortgage25", "mortgage26", "mortgage27", "mortgage28", "mortgage29", _
            "mortgage30", "mortgage31", "mortgage32", "mortgage33", "mortgage34", "mortgage35", _
            "mortgage36", "mortgage37", "mortgage38", "mortgage39", "mortgage40", "mortgage41", _
            "mortgage42", "mortgage43", "mortgage44", "mortgage45", "mortgage46", "mortgage47", _
            "mortgage48", "mortgage49")
                
    For fName = LBound(fileARR) To UBound(fileARR)
        If Dir("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx") <> "" Then _
            Workbooks.Open ("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx")
    Next fName
    
    'other code....
    
    For fName = LBound(fileARR) To UBound(fileARR)
        If Dir("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx") <> "" Then _
            Workbooks("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx").Close SaveChanges:=False
    Next fName
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    12-09-2010
    Location
    Grand Cayman
    MS-Off Ver
    Excel 2007
    Posts
    3

    Question Re: macro to open and close Excel workbooks

    Thank you. I really appreciate you taking the time to help me.

    Unfortunately your revised version fell apart at the same point as my original one.

    Error 9.
    Workbooks("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx").Close SaveChanges:=False
    I added
    Application.DisplayAlerts = False
    and

    Application.DisplayAlerts = True
    at the beginning and end of the code. Here is the final version:

    Option Explicit
    
    Sub Openspreadsheets()
    
    Application.DisplayAlerts = False
    
    Dim fileARR As Variant
    Dim fName As Long
    
    fileARR = Array("mortgage", "mortgage1", "mortgage2", "mortgage3", "mortgage4", "mortgage5", _
            "mortgage6", "mortgage7", "mortgage8", "mortgage9", "mortgage10", "mortgage11", _
            "mortgage12", "mortgage13", "mortgage14", "mortgage15", "mortgage16,", "mortgage17", _
            "mortgage18", "mortgage19", "mortgage20", "mortgage21", "mortgage22,", "mortgage23", _
            "mortgage24", "mortgage25", "mortgage26", "mortgage27", "mortgage28", "mortgage29", _
            "mortgage30", "mortgage31", "mortgage32", "mortgage33", "mortgage34", "mortgage35", _
            "mortgage36", "mortgage37", "mortgage38", "mortgage39", "mortgage40", "mortgage41", _
            "mortgage42", "mortgage43", "mortgage44", "mortgage45", "mortgage46", "mortgage47", _
            "mortgage48", "mortgage49")
                
    For fName = LBound(fileARR) To UBound(fileARR)
        If Dir("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx") <> "" Then _
            Workbooks.Open ("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx")
    Next fName
    
    'other code....
    
    For fName = LBound(fileARR) To UBound(fileARR)
        If Dir("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx") <> "" Then _
            Workbooks("C:\Users\Owner\Documents\MortAcc\" & fileARR(fName) & ".xlsx").Close SaveChanges:=False
    Next fName
    
    Application.DisplayAlerts = True
    End Sub
    I just don't understand it as both your coding and mine looks like it should do the job.
    Last edited by norman0000; 12-10-2010 at 02:18 PM. Reason: added [code]

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: macro to open and close Excel workbooks

    Please revise your post to add code tags around the code, as shown in my posts and demonstrated in my signature below.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: macro to open and close Excel workbooks

    I'm thinking it may be as simple as blowing through those errors. Add this near the top with all the other App settings:
    On Error Resume Next
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: macro to open and close Excel workbooks

    You don't want the path:
    Workbooks(fileARR(fName) & ".xlsx").Close SaveChanges:=False

  7. #7
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: macro to open and close Excel workbooks

    When you close a workbook, you're referring to a workbook in memory, not the workbook on disk, so you don't include the path.

    I can't imagine why you would want to open all the workbooks at once, rather than process them sequentially:
    Sub OpenSpreadsheets()
        Const sPath     As String = "C:\Users\Owner\Documents\MortAcc\"
        Dim i           As Long
        Dim sFile       As String
        Dim wkb         As Workbook
    
        For i = 0 To 49
            sFile = "mortgage" & WorksheetFunction.Text(i, "0;;")
            If Len(Dir(sPath & sFile)) Then
                Set wkb = Workbooks.Open(sPath & sFile)
    
                With wkb
                    ' process here, e.g.,
                    MsgBox .Worksheets(1).Name
                    ' ...
    
                    wkb.Close SaveChanges:=False
                End With
            End If
        Next i
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    12-09-2010
    Location
    Grand Cayman
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: macro to open and close Excel workbooks

    Many thanks for all your help. Removing the path did the trick and it now works perfectly.

    To answer the question about "why open all the workbooks at once".

    This control workbook extracts values from the various individual mortgage accounts. I originally tried to open and close them sequentially.
    The result however was that the value from the first workbook flashed on the screen for a second, but disappeared when the second value appeared.

    Thanks to everyone who helped me.

    Norman

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0