How to Get Dir$ to populate an Array

phxsportz

Well-known Member
Joined
Jun 11, 2006
Messages
1,985
Say I want to get all the worksheets in a given directory into an array ?.. How would I use, or Should I use DIR$ to do this ??

a simple
Code:
fnames= Dir$("*.xls")
only returns the first result
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
There may be a better way but this seems to work:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> GetFilenames()<br><br><SPAN style="color:#00007F">Dim</SPAN> fnames() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><br><SPAN style="color:#00007F">ReDim</SPAN> fnames(1)<br>fname = Dir("")<br><SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">Until</SPAN> fname = ""<br>    <SPAN style="color:#00007F">If</SPAN> Right(fname, 3) = "xls" <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> fnames(UBound(fnames) + 1)<br>        fnames(i) = fname<br>        i = i + 1<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    fname = Dir<br><SPAN style="color:#00007F">Loop</SPAN><br><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
You can use an array to hold the files once you loop through them, but I'm found of using collections (with which you can then use other methods like .Count, .Item, .Remove, etc) one-dimensional sets of data.

Try this:

Code:
Sub GetSomeFiles()
    ' Variable declarations
    Dim sDir As String      ' This is for the path/directory.
    Dim sFile As String     ' This is for the file(s).
    Dim i As Integer        ' This is a counter.
    Dim coll As Collection  ' This is how we'll store the files.
 
    ' Set a new instance of the collection (have to
    ' do this part first in order to use the collection).
    Set coll = New Collection
    ' Set the directory here.
    sDir = "C:\"
    ' Set the sFile variable to the Dir return (which is the
    ' sDir variable we just set).
    sFile = Dir(sDir)
 
    ' Loop through the files while the sFile string is not Null
    ' (it will eventually be set to Null later in the loop).
    Do While Len(sFile) <> 0
 
        ' Add the file name to the collection.
         coll.Add sFile
 
        ' To get the next file in the same directory,
        ' set sFile to Dir again, except this time without
        ' an argument in the Dir function. This tells VBA to
        ' get the next file in the same directory. When it runs
        ' out of files, it will return a Null value.
        sFile = Dir
 
    ' Loop (duh!)
    Loop
End Sub
 
Upvote 0
You can use an array to hold the files once you loop through them, but I'm found of using collections (with which you can then use other methods like .Count, .Item, .Remove, etc) one-dimensional sets of data.

Sir,
Thank you much for this code. It works beautifully!
I have a similar problem as the original post and this solved it perfectly.
 
Upvote 0
My apologies Sir Dude,

Just wanted to relay the proper amount of respect for the help received.
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top