Solved

Using an Array for Filenames in Excel VBA

Posted on 2002-03-06
2
438 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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…
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 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…

740 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