Solved

Using an Array for Filenames in Excel VBA

Posted on 2002-03-06
2
429 Views
Last Modified: 2007-11-27
Help,

I'm trying to use an array to list filenames in a particular directory in Excel, open the files or close the files.
I can get he file open, but when I close one of the files, I want all the files to close and am having problems with this.

Here's my code so far:

Dim filenames() As String, filenum As Integer
Dim i As Integer, 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)
    For i = 1 To FileCounter
        filenames(i - 1) = PathandFile
    Next i
    If opening = True Then
        Workbooks.Open FileName:=PathandFile, ReadOnly:=True
        Application.Run Macro:=searchFile + "!Auto_Open"
    Else
        For Each w In Application.Workbooks
        MsgBox w.name
        longname = sourcedir + w.name
        If member(longname, filenames) = True Then
            Application.Run Macro:=w.name + "!Auto_Close"
       
        w.Close (False)
        End If
        Next
    End If
    searchFile = Dir()
Loop

Any help would be appreciated.

Thanks!!

Tammi
0
Comment
Question by:Tammi
2 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 100 total points
Comment Utility
Your problem is here:

   ReDim Preserve filenames(FileCounter)
   For i = 1 To FileCounter
       filenames(i - 1) = PathandFile
   Next i

You are setting every entry in the array to the same filename each time, when you should be setting only the last entry to the new filename.  Replace this with:

   ReDim Preserve filenames(FileCounter)
   filenames(FileCounter) = PathandFile

Now, your lookup of the filename in the Member routine will work.
0
 

Author Comment

by:Tammi
Comment Utility
Thanks!!  This did the trick.

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now