You are here:

Excel/Automatic link to folders


QUESTION: Automatic link to folders
Is looking for a macro that lists all subfolders in a top folder and automatically make links to the folders.
Top Folder path = ActiveWorkbook path so that Excel spreadsheet can be copied to different locations without having to change the code.
The sheet of links used as an overview and quick access to the different folders

Grateful for help

ANSWER: Svein,

Here are several articles showing different approaches to recursively searching directories.  You can remove the code to list files and just use the directory results.
How To Search Directories to Find or List Files
HOW TO: Recursively Search Directories by Using FileSystemObject
How To Use FileSystemObject with Visual Basic

if you only want the next level directories (the directories one level below the starting directory - the thisworkbook.path) you can use

Sub AddDirectoryNames()
Dim i As Long, spath As String
Dim Dirname As String, ndir As Long, dirnames() As String
spath = ThisWorkbook.Path
If Right(spath, 1) <> "\" Then spath = spath & "\"
ndir = 1
ReDim dirnames(1 To ndir)
     Dirname = Dir(spath, vbDirectory Or vbHidden Or vbArchive Or vbReadOnly _
Or vbSystem)  ' Even if hidden, and so on.
     Do While Len(Dirname) > 0
        ' Ignore the current and encompassing directories.
        If (Dirname <> ".") And (Dirname <> "..") Then
         ' Check for directory with bitwise comparison.
         If GetAttr(spath & Dirname) And vbDirectory Then
         dirnames(ndir) = Dirname
         ndir = ndir + 1
         ReDim Preserve dirnames(1 To ndir)
         End If
        End If
        Dirname = Dir()
' now you have a list of top level directories in the array dirnames
For i = 1 To UBound(dirnames) - 1
 ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:=dirnames(i), _
End Sub

that was tested and worked for me.

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: Your code worked perfectly
But if i want to include subfolderstto the list, how do i do that.
I've tried different, but I'm obviously not good enough.

Regard Svein


this worked for me.

Insert a new module in your workbook, then insert this code.

run Master to get your list of hyperlinks.

Option Explicit
Public DirArray() As String
Public Dircount As Long

Sub Master()
Dim i As Long
ReDim DirArray(1 To 1)
Dircount = 1
FindFiles ThisWorkbook.path
For i = 1 To UBound(DirArray) - 1
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:=DirArray(i), _

End Sub

Function FindFiles(spath As String)

Dim FileName As String   ' Walking filename variable.
Dim DirName As String    ' SubDirectory Name.
Dim dirNames() As String ' Buffer for directory name entries.
Dim nDir As Integer      ' Number of directories in this path.
Dim i As Integer         ' For-loop counter.

On Error GoTo sysFileERR

If Right(spath, 1) <> "\" Then spath = spath & "\"
     ' Search for subdirectories.
 nDir = 1
 ReDim dirNames(1 To nDir)
 DirName = Dir(spath, vbDirectory Or vbHidden _
        Or vbArchive Or vbReadOnly Or vbSystem)  ' Even if hidden, and so on.
 Do While Len(DirName) > 0
 ' Ignore the current and encompassing directories.
  If (DirName <> ".") And (DirName <> "..") Then
   ' Check for directory with bitwise comparison.
   If GetAttr(spath & DirName) And vbDirectory Then
      dirNames(nDir) = DirName
      DirArray(Dircount) = spath & DirName
      nDir = nDir + 1
      Dircount = Dircount + 1
      ReDim Preserve dirNames(1 To nDir)
      ReDim Preserve DirArray(1 To Dircount)
   End If
  End If
  DirName = Dir()  ' Get next subdirectory.
Debug.Print spath, nDir
 ' If there are sub-directories..
 If nDir > 1 Then
    ' Recursively walk into them
   For i = 1 To nDir - 1
      FindFiles spath & dirNames(i) & "\"
   Next i
 End If

     Exit Function
If Right(DirName, 4) = ".sys" Then
  Resume sysFileERRCont ' Known issue with pagefile.sys
  MsgBox "Error: " & Err.Number & " - " & Err.Description, , _
        "Unexpected Error"
  Resume AbortFunction
End If
End Function

Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 All rights reserved.