Solved

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

Posted on 2002-03-28
11
495 Views
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"
    Else
         For Each w In Application.Workbooks
         longname = sourcedir + w.name
         If member(longname, filenames) = True Then
         Application.Run Macro:=w.name + "!Auto_Close"
         Application.DisplayAlerts = False
         w.Close SaveChanges:=False
    End If
    Next
    End If
    searchfile = Dir()
Loop
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
    Next
    member = found
End Function
0
Comment
Question by:Tammi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6903247
what's the error?
0
 

Author Comment

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

Expert Comment

by:bobbit31
ID: 6903304
odd, i just have:
w.Close SaveChanges:=False
   
and it doesn't prompt me (w/o that line it does)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

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

Author Comment

by:Tammi
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.
0
 

Author Comment

by:Tammi
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.
0
 
LVL 44

Expert Comment

by:bruintje
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

:O)Bruintje
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6903577
or open them with this line added

ActiveWorkBook.RunAutoMacros xlAutoDeactivate

HTH:O)Bruintje
0
 
LVL 49

Accepted Solution

by:
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
0
 

Author Comment

by:Tammi
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7659695
Tammi,
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
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

617 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