XL: Visual Basic Code to Use Instead of DIRECTORIES()

Last reviewed: September 2, 1997
Article ID: Q113491
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows

SUMMARY

This article contains sample Visual Basic for Applications code that you can use to duplicate the behavior of the DIRECTORIES() function that was included in the Microsoft Excel 4.0 Filefns.xla add-in.

NOTE: The Filefns.xla add-in is not included in Microsoft Excel 5.0, 7.0, and 97.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Sample Visual Basic Code

' The main procedure calls dir_test and passes it to the specified
' directory.

Sub main()
   'Always make sure the path ends with a backslash.
   'Calls dir_test and passes it the directory to check
   dir_test "c:\"
End Sub

' The dir_test procedure returns an array of all subdirectories contained
' in the specified directory. To do this, it creates the array dir_array;
' dir_array is declared as Static to hold its values after the macro has
' finished.

Sub dir_test(directory_text)
   'Dimensions a variable to hold the temp Directory name
   Dim temp_var As String

   'Dimensions a dynamic array to hold the Directory Array
   Static dir_array() As String

   'Turns off Error Checking
   On Error Resume Next

   'Call the DIR function and returns the first item the Directory
   temp_var = Dir(directory_text, vbDirectory)

   'Initializes the variable for building the array
   counter = 0

'Set a loop until the DIR function returns
Do Until temp_var = ""
    'Temp_var stores the individual Directory name
    temp_var = Dir()
    'Checks to see if temp_var is an empty string
    If temp_var <> "" Then
       'The following code will create an array of the directories
       If GetAttr(directory_text & temp_var) And vbDirectory then
            'enlarge the array to hold a new item
            ReDim Preserve dir_array(counter)
            'add directory to the array
            dir_array(counter) = temp_var
            'This line just shows the variable was added to the array
            'You can comment this line out when using this function
            MsgBox dir_array(counter)
            'Increase counter to make room for the next directory
            counter = counter + 1
       End If
    End If
Loop
End Sub

REFERENCES

For more information about Filefns.xla and the Visual Basic equivalents for those functions, query on the following words in the Microsoft Knowledge Base:

   Filefns.xla and add-in and maintenance


Additional query words: 5.00 7.00 8.00 97 directorybrowse Array Dir
list linked_directory
Keywords : kbprg PgmHowTo kbprg
Version : 5.00 5.00c 7.00 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.