Need help closing multiple files using the Dir function in Excel VBA

Posted on 2002-03-28
Last Modified: 2008-02-01
I have an excel spreadsheet that opens a code sheet and then if exists, multiple branch*.xls files.  I previously used the Application.Filesearch function, but this no longer works with Windows 2000.  I am now using the dir function.  I can get all the files open, but when I exit Excel, I need to close these branch*.xls files without asking me whether I want to save, they are read only anyway.  Below is the code I am using.  Any help would be appreciated.

Sub HandleLocalizations(sourcedir As String, opening As Boolean)

'  allow the opening and run the auto open code for mulitple branch.xls files
'  these files must all be in the same directory as the point schedule tool and
'  must be named in the form branch*.xls, open read only in case on network
'  added flag for opening or closing, when closing, will call auto_close of
'  branch*.xls file(s) and will close w/o prompting for save, even if changes made

Dim filenames() As String, filenum As Integer
Dim i As Integer, justfilename As String, longname As String
FileCounter = 0
sourcedirfile = sourcedir + "\branch*.xls"
searchfile = Dir(sourcedirfile)
Do While searchfile <> ""
    PathandFile = sourcedir + searchfile
    FileCounter = FileCounter + 1
    ReDim Preserve filenames(FileCounter)
    filenames(FileCounter) = PathandFile
    If opening = True Then
        Workbooks.Open FileName:=PathandFile, ReadOnly:=True
        Application.Run Macro:=searchfile + "!Auto_Open"
         For Each w In Application.Workbooks
         longname = sourcedir +
         If member(longname, filenames) = True Then
         Application.Run + "!Auto_Close"
         Application.DisplayAlerts = False
         w.Close SaveChanges:=False
    End If
    End If
    searchfile = Dir()
End Sub

Function member(item, list) As Boolean
' returns true if item is member of collection list, false if not
    Dim found As Boolean
    found = False
    For Each i In list
            If i = item Then
                found = True
            Exit For
            End If
    member = found
End Function
Question by:Tammi
  • 4
  • 3
  • 2
  • +1
LVL 18

Expert Comment

ID: 6903247
what's the error?

Author Comment

ID: 6903264
There is no error, it just asks to close the file and I want it to close without asking.
LVL 18

Expert Comment

ID: 6903304
odd, i just have:
w.Close SaveChanges:=False
and it doesn't prompt me (w/o that line it does)
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 44

Expert Comment

ID: 6903333
Hi Tammi, does this prompt too ? > w.Close False

Author Comment

ID: 6903431
I have w.Close SaveChange:=False and I still get the prompt.  I also used w.Close (False) and still got the prompt.

Author Comment

ID: 6903433
Currently I have three different branch*.xls files that it finds, one doesn't have an Auto_Close function in it, and that one closes without prompting, the other two I get the prompt to save.
LVL 44

Expert Comment

ID: 6903570
so it's the auto_close playing up?

you could add a line there

ActiveWorkbook.Saved = True

i don't know if it works if you use it on another place in your code before closing like

Application.DisplayAlerts = False
ActiveWorkbook.Saved = True
w.Close False

LVL 44

Expert Comment

ID: 6903577
or open them with this line added

ActiveWorkBook.RunAutoMacros xlAutoDeactivate

LVL 49

Accepted Solution

DanRollins earned 200 total points
ID: 7637946
Hi Tammi,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept bruintje's comment(s) as an answer.

Tammi, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Author Comment

ID: 7656577
This was so long ago I forgot what I did to fix the problem, but I know I didn't use any of the answers listed.
LVL 49

Expert Comment

ID: 7659695
You did not bother to read my post.  What part of:

    DO NOT accept this comment as an answer.

is unclear?  And why have you ruined by GPA by insulting me with a C?  

-- Dan

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 130
Passing a Text Box name to a Sub 6 99
TT Auto Dashboard 13 95
How to read File Date Created using VB6 8 50
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

791 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