Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using an Array for Filenames in Excel VBA

Posted on 2002-03-06
2
Medium Priority
?
451 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:
Brendt Hess earned 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

610 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