Microsoft KB Archive/74493

= XL: Macro to Place Filenames in Given Directory on Worksheet =

Article ID: 74493

Article Last Modified on 11/16/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q74493





SUMMARY
The sample macros in this article place the names of all specified file types (specified by file name extension, for example .XLS) for a specified directory into a column on a worksheet.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. Before you run these macros, open a new worksheet and select the cell of the range into which you want the file names to be placed.

To list different file types, you must modify the sample macros by changing the argument in the Dir function. To return all Microsoft Excel add-in macros, replace &quot;*.XLS&quot; with &quot;*.XLA,&quot; and so on. The specified directory can be any valid directory. To search a different folder, change  to the folder containing your Excel workbook files.   In a new a module sheet, type the following: Sub ListFiles F = Dir(&quot;C:\\*.XLS&quot;) Do While Len(F) > 0 ActiveCell.Formula = F              ActiveCell.Offset(1, 0).Select F = Dir Loop End Sub  To run the macro, open a new worksheet. Select cell A1. On the Tools menu, point to Macro, and then click Macros. In the list of available macros, select the ListFiles macro, and then click Run.

All workbook files located in the  directory will be listed in column A in the worksheet.

To modify the macro so that it returns the file names in a row rather than in a column, change this line ActiveCell.Offset(1, 0).Select to this: ActiveCell.Offset(0, 1).Select

Additional query words: directories folder XL2000 xl2002 xl97

Keywords: kbcode kbhowto kbmacro KB74493

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.