Solved

Using an Array for Filenames in Excel VBA

Posted on 2002-03-06
2
439 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
[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
2 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 100 total points
ID: 6845423
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
ID: 6868325
Thanks!!  This did the trick.

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

707 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